1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 打开excel显示php拓展名 phpexcel 导出excel 因为文件格式或文件扩展名无效 请确

打开excel显示php拓展名 phpexcel 导出excel 因为文件格式或文件扩展名无效 请确

时间:2023-12-10 20:04:12

相关推荐

打开excel显示php拓展名 phpexcel 导出excel 因为文件格式或文件扩展名无效 请确

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

$objPHPExcel =newPHPExcel();

$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打开现有的格式的excel又没有问题。

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

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

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

之后添加:

ob_end_clean();

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

$objPHPExcel =newPHPExcel();

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

打开excel显示php拓展名 phpexcel 导出excel 因为文件格式或文件扩展名无效 请确定文件未损坏 并且文件扩展名与文件的格式匹配...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。