this is my simple code to create an xlsx file and open it with openoffice
require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel.php');
require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');
$this->load->library('excel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->SetCellValue('A1', 'Calendario Attivit');
$filename = "prova".".xlsx";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="'.$filename.'"');
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
the file is downloaded but when open it i get this:
someone can help me ?
Try to add ob_end_clean() before the output :
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
Related
Below is my simple test php. When I run this nothing at all happens.
require_once('/var/www/html/Classes/PHPExcel.php');
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)
->setCellValue('A1','Hello')
->setCellValue('B1','World');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="/var/www/html/Examples/test.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
exit;
Any ideas of why this does not work. No redirect at all and not file either.
With skimming PHPEXCEL documentation sample something like the following could work :
<?php
date_default_timezone_set('America/Los_Angeles');
require_once('/var/www/html/Classes/PHPExcel.php');
$sheet = array(
array(
'a1 Hello',
'b1 World',
'c1 here',
'd1 test',
)
);
$doc = new PHPExcel();
$doc->setActiveSheetIndex(0);
$doc->getActiveSheet()->fromArray($sheet, null, 'A1');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// Do your stuff here
$writer = PHPExcel_IOFactory::createWriter($doc, 'Excel5');
$writer->save('php://output');
?>
Important Note Dont kill the process by exit();
I use latest PHPExcel library for creating .xls, .xlsx files. When i try to output created file like this:
ob_end_clean();
$file = 'test.xls';
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
everything is fine until I try to create and output any charts. So i changed my code according to example:
ob_end_clean();
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save(str_replace('.php', '.xlsx', __FILE__));
exit;
This is working, but the file is not downloaded. So i modified code to output created file:
ob_end_clean();
$file = 'test.xlsx';
header('Content-Type: Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
And this is not working, ends up with ERR_INVALID_RESPONSE.
Also when i store excel file with charts with second code block, it's not compatibile with MS Excel 2016. It says the file is corrupted and the data were replaced or deleted. What am I missing? Thanks a lot.
I figured out one simple solution, first step is to store created .xlsx file, second is to download file with header("Location:"). Redundant files are deleted with array_map function.
array_map('unlink', glob( __DIR__."/*.xlsx"));
$file = 'test.xlsx';
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save($file);
header("Location: $file");
I have this controller which renders my data from database to the PHPExcel library.
https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
this is my controller :
public function downloadTournamentData() {
$this->load->model("Tournament");
$var_data = array();
$var_data['tournament'] = $this->Tournament->getTournamentData();
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test');
$this->excel->getActiveSheet()->setCellValue('A1', 'member_username');
$this->excel->getActiveSheet()->setCellValue('B1', 'member_name');
$this->excel->getActiveSheet()->setCellValue('C1', 'member_phone');
$this->excel->getActiveSheet()->setCellValue('D1', 'member_email');
$this->excel->getActiveSheet()->setCellValue('E1', 'member_idcard');
$this->excel->getActiveSheet()->setCellValue('F1', 'Date_created');
$this->excel->getActiveSheet()->setCellValue('G1', 'team_name');
$this->excel->getActiveSheet()->setCellValue('H1', 'warnet_name');
$this->excel->getActiveSheet()->setCellValue('I1', 'warnet_cp');
$this->excel->getActiveSheet()->setCellValue('J1', 'warnet_phone');
$this->excel->getActiveSheet()->setCellValue('K1', 'region_name');
$this->excel->getActiveSheet()->setCellValue('L1', 'City');
$cell_inc = 2;
foreach($var_data['tournament'] as $k => $v) {
$this->excel->getActiveSheet()->setCellValue('A'.$cell_inc, $v['member_username']);
$this->excel->getActiveSheet()->setCellValue('B'.$cell_inc, $v['member_name']);
$this->excel->getActiveSheet()->setCellValue('C'.$cell_inc, $v['member_phone']);
$this->excel->getActiveSheet()->setCellValue('D'.$cell_inc, $v['member_email']);
$this->excel->getActiveSheet()->setCellValue('E'.$cell_inc, $v['member_idcard']);
$this->excel->getActiveSheet()->setCellValue('F'.$cell_inc, $v['Date_created']);
$this->excel->getActiveSheet()->setCellValue('G'.$cell_inc, $v['team_name']);
$this->excel->getActiveSheet()->setCellValue('H'.$cell_inc, $v['warnet_name']);
$this->excel->getActiveSheet()->setCellValue('I'.$cell_inc, $v['warnet_cp']);
$this->excel->getActiveSheet()->setCellValue('J'.$cell_inc, $v['warnet_phone']);
$this->excel->getActiveSheet()->setCellValue('K'.$cell_inc, $v['region_name']);
$this->excel->getActiveSheet()->setCellValue('L'.$cell_inc, $v['City']);
$cell_inc++;
}
date_default_timezone_set("Asia/Jakarta");
$this_date = date("Y-m-d");
$filename='pb_turnamen_data-'.$this_date.'.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel; charset=UTF-8'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
}
It does download the excel file. Only problem is that the data is mixed up and it's all wrong.
Why is this happening? Is there something wrong with my controller?
Please provide solution...
Add
ob_end_clean();
after
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
final code is
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
I am trying to write data to a template and download it.
If i use the sample files as a template, it works but once I use my own it says its corrupt.
Any thoughts?
My code is:
<?php
require_once 'PHPExcel.php';
// Create new PHPExcel object
$fileName = '31docproperties.xlsx'; //location of template for PID type
$fileType = 'Excel2007';
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B4', 'Test Data');
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>
I have a webpage that has a radio group as the options for the file format you wish to save. Options are:
.xls
.xlsx
.csv
All work but the .csv as it also adds the page HTML to the bottom of the file.
Here is what I'm trying (code snippets to show functionality):
// Creating the format
$data = $this->getQueryResults();
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle("Report");
$objPHPExcel->getProperties()->setCreator("me");
$objPHPExcel->getProperties()->setLastModifiedBy("me");
$objPHPExcel->getProperties()->setSubject("Report Stuff");
$objPHPExcel->getProperties()->setDescription("Report Stuff");
// Next I iterate through the data array
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
// check the radio option selected for the file format
if($this->radioXLS->Checked) {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
header('Cache-Control: max-age=0');
}
if($this->radioXLSX->Checked) {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
header('Cache-Control: max-age=0');
}
if($this->radioCSV->Checked) {
ob_end_clean(); // add/removing this does nothing
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter(',');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
ob_end_clean(); // add/removing this does nothing
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
header('Cache-Control: max-age=0');
}
$objWriter->save('php://output');
Any thoughts as to why it appends the page HTML to the .csv file?
On a side note this is a Prado project if that matters
UPDATE:
A little more...
I have a webpage that generate a report in a tablature format (Think table/grid). On the same page I have the option to save the date in the tablature format to an Excel .xls (somehow .xlsx is not working now, ugh...). The user has the option to save the file in .xls .xlsx .csv, when clicked the file downloads from that page.
Would this cause the webpage already rendered to be added to the output via: php://output?
UPDATE - Solution:
Yep after looking at the excel files it's also adding the webpage HTML. I have also looked at the output buffer PHP functions but still nothing is working
while(ob_get_level() > 0) {
ob_end_clean();
}
if($this->radioCSV->Checked) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter(',');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
} elseif($this->radioXLSX->Checked) {
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
} else {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
}
$objWriter->save('php://output');
exit();
Writing to php://output is exactly the same as doing a plain echo statement. The output of $objWriter->save() will be added to everything else that's echoed or lies outside PHP blocks (<?php ... ?>).
An example:
This is right:
<?php
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle("Report");
// ...
$objWriter->save('php://output');
?>
This is wrong:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="es">
<head><title>Export to Excel</title>
</head>
<body>
<?php
echo '<h1>Export to Excel</h1>';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle("Report");
// ...
$objWriter->save('php://output');
?>
</body>
</html>
Have you verified that the Excel files don't contain the HTML? They might just be ignored by Excel. Does adding exit(); after the last line change anything?