PHPExcel return a corrupted file

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?

Upvotes: 5

Views: 17613

Answers (3)

Muhammad Abdullah
Muhammad Abdullah

Reputation: 4465

User **ob_end_clean(); ** Before u set the header

like this :

     ob_end_clean(); 
              header('Content-Type: application/vnd.ms-excel'); //mime type

            header("Content-Disposition: attachment; filename=\"filename.xls\"");
            header("Cache-Control: max-age=0");
            $objWriter = PHPExcel_IOFactory::createWriter($object, 'Excel5');
              $objWriter->save('php://output');

Upvotes: 3

Murat Nurkassimov
Murat Nurkassimov

Reputation: 31

Try ob_end_clean(); after $objWriter->save('php://output'); instead of header if the selected solution doesn't work for you

example:

    $filename = $name.".xls";
    ob_end_clean();
    header( "Content-type: application/vnd.ms-excel" );
    header('Content-Disposition: attachment;filename='.$filename .' ');
    header("Pragma: no-cache");
    header("Expires: 0");
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    ob_end_clean();

Upvotes: 0

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();

Upvotes: 13

Related Questions