Error while opening an Excel file that generated using phpExcel

南楼画角 提交于 2020-01-11 14:15:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!