PHP在本机可以正常导出Excel,在上Linux就提示ERR_EMPTY_RESPONSE,下面说一下怎么不降版本还能正常导出Excel,解决上面的问题。
使用导出Excel的时候显示错误页面提示如下,这时候发现上传Excel都可以正常读取数据,只是导出的时候不可以。

我这里使用的PHP版本是7.1。PHPExcel版本用的是1.8.1。github地址:
https://github.com/PHPOffice/PHPExcel/tree/1.8.1。可以看到作者已经3年没更新过了。

将PHPExcel里面的Class内容都拷贝到thinkphp的vendor目录下。重命名为PHPExcel。也就是将之前老的插件换成这个1.8.1的版本。

具体怎么写Excel和引入插件我就不说了。说下代码方面的写法。
问题其实集中出现在Excel输出这里。(下面是成功导出Excel的写法,前面生成Excel的部分略去了)定义头部后直接输出。
$filename = "模板文件".date("YmdHis").rand(1000,9999);$filename = iconv("utf-8", "gb2312", $filename);header("Pragma: public");header("Expires: 0");header("Cache-Control:must-revalidate, post-check=0, pre-check=0");header("Content-Type:application/force-download");header("Content-Type:application/vnd.ms-execl");header("Content-Type:application/octet-stream");header("Content-Type:application/download");;header('Content-Disposition:attachment;filename='.$filename.'.xls');header("Content-Transfer-Encoding:binary");$objWriter- save('php://output');


也可以使用PhpSpreadsheet来读写Excel。
github:https://github.com/PHPOffice/PhpSpreadsheet
文档:https://phpspreadsheet.readthedocs.io/en/develop/


以上就是关于PHP7导出Excel报ERR_EMPTY_RESPONSE的相关解决步骤和方法,感谢大家的阅读和对ZaLou.Cn的支持。