报错:因为文件格式或文件扩展名无效,请确定文件未损坏,并且文件扩展名与文件的格式匹配。

在代码末加上ob_end_clean();如下。

$objPHPExcel = new PHPExcel();
$filename = "test.xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"{$filename}\"");
header('Cache-Control: max-age=0');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
header ('Cache-Control: cache, must-revalidate'); 
header ('Pragma: public'); 
 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');