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/
免责声明:文章源自网络,版权归原作者所有,如有侵犯联系删除。