I want to write a reference xls file and retrieve the result. When I tested with a single file, it works but when the reference file is complex, this returns me a corrupt file with with kinds of data:
Which is at the beginning like this:
My php file is:
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once '../phpexcel/Classes/PHPExcel.php';
$objPHPexcel = PHPExcel_IOFactory::load('../upload/ref/fileref.xls');
$objWorksheet = $objPHPexcel->getActiveSheet();
$objWorksheet = $objPHPexcel->getActiveSheet();
$objWorksheet->getCell('B1')->setValue('toto');
$objWorksheet->getCell('B3')->setValue('toto');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="newfiletodownload.xls"');;
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
$objWriter->save('php://output');
Do you have solution ? Thanks, Neyoh
Can you try to put ob_end_clean();
just before the header(...)
part?
This worked for someone with the same problem. See the comment at the accepted answer of this post: PHPExcel generated excel file not working. (File format or extension is invalid ).
Note:
Using ob_end_clean()
is just a work around for this problem. The main problem is that additional and unnecessary outputs are sent to the PHPExcelWriter
that causes the file to get messed up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With