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>';
}
发布评论