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

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...