Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel return a corrupted file

Tags:

php

phpexcel

If I save it to file:

$objWriter->save("test.xls");

And then download from server, I can open file without any problems.

But when I try to save into php://output, got corrupted file like described in this question.

I tried this:

ob_end_clean();
header( "Content-type: application/vnd.ms-excel" );
header('Content-Disposition: attachment; filename="test.xls"');
header("Pragma: no-cache");
header("Expires: 0");

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save('php://output');
exit;

ob_end_clean(); doesn't help.

I tried PHPExcel_Writer_Excel2007 and PHPExcel_Writer_Excel5. Same problem.

UPD: as I can see, excell file has BOM at start ("EF BB BF"). But I've checked all my script, they don't have BOM symbol. So, does it means that PXPExcell add BOM?

like image 581
Suvitruf - Andrei Apanasik Avatar asked Dec 15 '22 09:12

Suvitruf - Andrei Apanasik


1 Answers

I added ob_end_clean(); also after headers section. And BOM symbol disappeared.

ob_end_clean();
header( "Content-type: application/vnd.ms-excel" );
header('Content-Disposition: attachment; filename="test.xls"');
header("Pragma: no-cache");
header("Expires: 0");
ob_end_clean();
like image 95
Suvitruf - Andrei Apanasik Avatar answered Dec 25 '22 11:12

Suvitruf - Andrei Apanasik