我正在使用PHPExcel以xlsx格式下载文件。我使用的是codeigniter框架。以前我正在下载xls格式。此时文件已成功下载。如果我试图以xlsx格式下载,它会给出如下错误图像。在此处输入图像描述
我的代码是:
$this->load->library('Excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('Sales Data');
$filename='Salesdata.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
当我以xls格式添加时没有错误。当我添加为filename.xlsx时,它给出了错误。请提供任何解决方案。
您可以继续使用php输出缓冲区和codeigniter下载助手例如:-
$filename='Salesdata.xlsx';
ob_start();
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
$this->load->helper('download');
$excelFileContents = ob_get_clean();
force_download($filename, $excelFileContents);