当前位置:Gxlcms > PHP教程 > php下载excel打不开了怎么办

php下载excel打不开了怎么办

时间:2021-07-01 10:21:17 帮助过:17人阅读

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

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

php下载excel文件:

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

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

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

代码如下:

  1. if (file_exists(CACHE_PATH .
  2. $file_name)){
  3. //$this->logger->error('file
  4. realpath:'.realpath(CACHE_PATH . $file_name));
  5. header( 'Pragma: public'
  6. );
  7. header( 'Expires: 0' );
  8. header( 'Content-Encoding: none'
  9. );
  10. header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0'
  11. );
  12. header( 'Cache-Control: public' );
  13. header( 'Content-Type:
  14. application/vnd.ms-excel');
  15. header( 'Content-Description: File
  16. Transfer' );
  17. header( 'Content-Disposition: attachment; filename=' .
  18. $file_name );
  19. header( 'Content-Transfer-Encoding: binary' );
  20. header( 'Content-Length: ' . filesize ( CACHE_PATH . $file_name ) );
  21. readfile ( CACHE_PATH . $file_name );
  22. } else
  23. {
  24. $this->logger->error('export model :'.$id.' 错误:未生产文件');
  25. echo '<script>alert(\'export error, file not
  26. exists!\')</script>';
  27. }

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

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

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

人气教程排行