网络编程 
首页 > 网络编程 > 浏览文章

php下载excel无法打开的解决方法

(编辑:jimmy 日期: 2024/10/17 浏览:3 次 )

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>';
  }

上一篇:生成随机字符串和验证码的类的PHP实例
下一篇:php calender(日历)二个版本代码示例(解决2038问题)
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。