首页 > 编程知识 正文

普通excel文件的后缀,表格文件格式无效

时间:2023-05-05 04:53:38 阅读:286487 作者:3732

phpexcel导出excel:打开出现这个错误,强制打开是乱码

$objPHPExcel = new PHPExcel();$filename = "test.xls"; header('Content-Type: application/vnd.ms-excel');header("Content-Disposition: attachment;filename="{$filename}"");header('Cache-Control: max-age=0');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');

如上代码生成的test.xls在用excel打开时,会报“因为文件格式或文件扩展名无效,请确定文件未损坏,并且文件扩展名与文件的格式匹配”的问题。

刚开始怀疑是office的问题,但是office打开现有的2003格式的excel又没有问题。

所以最后有看代码到底是哪块发生的问题。

通过搜索最后发现,需要在:

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

之后添加:

ob_end_clean();

最后贴上测试无误的代码,如下:

$objPHPExcel = new PHPExcel();$filename = "test.xls"; header('Content-Type: application/vnd.ms-excel');header("Content-Disposition: attachment;filename="{$filename}"");header('Cache-Control: max-age=0');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');ob_end_clean();$objWriter->save('php://output');

转载:http://www.pianshen.com/article/2589196424/

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。