问题
I am trying to generate an Excel file using phpExcel library in Codeigniter. The problem is that while downloading the file it shows characters in in undifined formats
While opening the downloaded file it shows the message that is an different format, please refer the pic

and while opening the file content as

and my code stuff is:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->library('excel');
$sheet = new PHPExcel();
$objWorkSheet = $sheet->createSheet();
$sheet->getProperties()->setTitle('JDI Problem Improvement Strip')->setDescription('JDI Problem Improvement Strip');
$sheet->setActiveSheetIndex(0);
$sheet_writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$sheet_writer->save('php://output');
回答1:
Are you creating Excel 2007 files? If so try using mimetype application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
and file extension .xslx
refer - PHP Excel - The file you are trying to open .xls is in a different format than specified by the file extension
回答2:
ob_end_clean();
ob_start();
It cleans the buffer and get correct output. At least for me it resolve the issue.
来源:https://stackoverflow.com/questions/27352988/error-while-opening-an-excel-file-that-generated-using-phpexcel