输入banner图图片脚本导航/分类

php下载excel打不开了怎么办

php下载excel打不开的解决办法:1、在下载的过程中不要输出任何非文件信息;2、输出的excel格式一定要和后缀名保存一致;3、检查文件是否下载完整即可。

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php下载excel文件:

1、在下载的过程中不要 输出任何非文件信息,比如 echo log信息。

否则下载后的文件无法打开,提示格式错误或者文件被破坏。

2、 输出的excel格式一定要和后缀名保存一直,否也会提示格式错误或者文件被破坏

代码如下:

if (file_exists(CACHE_PATH .
$file_name)){
//$this->logger->error('file
realpath:'.realpath(CACHE_PATH . $file_name));
header( 'Pragma: public'
);
header( 'Expires: 0' );
header( 'Content-Encoding: none'
);
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0'
);
header( 'Cache-Control: public' );
header( 'Content-Type:
application/vnd.ms-excel');
header( 'Content-Description: File
Transfer' );
header( 'Content-Disposition: attachment; filename=' .
$file_name );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize ( CACHE_PATH . $file_name ) );
readfile ( CACHE_PATH . $file_name );
} else
{
$this->logger->error('export model :'.$id.' 错误:未生产文件');
echo '<script>alert(\'export error, file not
exists!\')</script>';
}

3、文件出错了,请检查文件是否下载完整。

推荐学习:《PHP视频教程》

以上就是php下载excel打不开了怎么办的详细内容,更多请关注gxlcms其它相关文章!