0

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 enter image description here

and while opening the file content as

enter image description here

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');
VishnuPrasad
  • 1,078
  • 5
  • 17
  • 36
  • which excel version you are trying to create? 2007? – Vinoth Babu Dec 08 '14 at 07:07
  • Open the file in a text editor. Look for leading/trailing whitespace characters, any BOM header, any plaintext PHP error messages or any html markup in the file, then find where your script is echoing those into the output stream – Mark Baker Dec 08 '14 at 09:16

2 Answers2

0

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

Community
  • 1
  • 1
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
0
ob_end_clean(); 
ob_start();

It cleans the buffer and get correct output. At least for me it resolve the issue.

Darshan Kini
  • 114
  • 8