jemz
November 2, 2014, 11:04am
1
Hi, why is it that when I am exporting my data to excel it always shows zero bytes ?
here is my code
createreport.php–>this is resides in c:\wamp\www\testexcel\
require_once dirname(__FILE__) . '/phpexcel/Classes/PHPExcel.php';
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
$objPHPExcel = new PHPExcel();
$result = mysql_query("select * from employee");
$headings = array("ID","employeeid","employeefirstname","employelastname");
$headingrow = 1;
$colheader = 'A';
foreach ($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($colheader.$headingrow, $heading);
$colheader++;
}
$row = 2;
while($row_data = mysql_fetch_assoc($result)) {
$col = 0;
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
$objPHPExcel->getActiveSheet()->setTitle('test');
$objPHPExcel->setActiveSheetIndex(0);
$filename = dirname(__FILE__).'/reports/testrpt-'.date('YmdHis').'.php';
$objWriter->save(str_replace('.php', '.xlsx', $filename));
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
?>
cpradio
November 2, 2014, 11:15am
2
When you save the file, you are renaming its extension from php to xlsx, but your headers will still be using the php extension (which wasn’t saved).
Thus the php extension is 0 bytes and the xlsx extension will be your actual file. Replace the following two line:
$filename = dirname(__FILE__).'/reports/testrpt-'.date('YmdHis').'.php';
$objWriter->save(str_replace('.php', '.xlsx', $filename));
With
$filename = dirname(__FILE__).'/reports/testrpt-'.date('YmdHis').'.xlsx';
$objWriter->save($filename);
and it should work.
jemz
November 2, 2014, 11:44am
4
@cpradio ,can I ask is there a way not to save the excel file to my server just to download it to the client ?
Thank you in advance.
cpradio
November 2, 2014, 11:52am
5
Yes, from what I’ve read, the following should work:
$filename = dirname(__FILE__).'/reports/testrpt-'.date('YmdHis').'.xlsx';
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary ");
$objWriter->save('php://output');
By saving it to php://output you are telling it to stream the data to the output stream of PHP and not to an actual file.
jemz
November 2, 2014, 12:01pm
6
Okay it’s working now thank you.by the way is this will work in any types of file like image ?
Yes, other than you may want to change the Content-Transfer-Encoding whenever your file is not binary (such as a text file, csv, etc). But it should work for most situations.
system
Closed
February 1, 2015, 11:05pm
8
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.