php下載excel文件,
1、在下載的過程中不要 輸出任何非文件信息,比如 echo log信息。 否則下載后的文件無法打開,提示格式錯(cuò)誤或者文件被破壞。
2、 輸出的excel格式一定要和后綴名保存一直,否也會(huì)提示格式錯(cuò)誤或者文件被破壞
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.' 錯(cuò)誤:未生產(chǎn)文件');
echo '<script>alert(\'export error, file not exists!\')</script>';
}