PHPExcel返回损坏的文件

时间:2015-12-20 14:06:22

标签: php phpexcel

如果我将其保存到文件:

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

然后从服务器下载,我可以毫无问题地打开文件。

但是当我尝试保存到php://output时,会遇到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();没有帮助。

我尝试了PHPExcel_Writer_Excel2007PHPExcel_Writer_Excel5。同样的问题。

UPD:正如我所看到的,excell文件在开始时有BOM(" EF BB BF")。但是我已经检查了我的所有脚本,他们没有BOM符号。那么,这是否意味着PXPExcell添加BOM?

2 个答案:

答案 0 :(得分:0)

如果选定的解决方案不适合您,请在ob_end_clean();之后尝试$objWriter->save('php://output');而不是标题

示例:

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

答案 1 :(得分:0)

用户** ob_end_clean(); **在设置标题之前

像这样:

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