Error while opening an Excel file that generated using phpExcel - php

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');

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

ob_end_clean();
ob_start();
It cleans the buffer and get correct output. At least for me it resolve the issue.

Related

How to fix Exporting Data to Excel, unable to export into .XLSX. PHPExcel

I am trying to export data into .XLSX excel format but unable to do it because of this error:
Message: ZipArchive::close(): Failure to create temporary file: No such file or directory
Filename: Writer/Excel2007.php
Here is my code that makes it to .XLSX format
$filename='Students Balance Sheet.xlsx';
ob_start();
$objWriter = PHPExcel_IOFactory::createWriter($object, '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);
I have also tried this code, but the problem is there is an invalid response, the page is invalid
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel2007');
$object_writer->save('php://output');
Before, I am able to export but it is only .XLS the content of the excel is unreadable or it looks like corrupted using this code:
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Students Balance Sheet.xlsx"');
$object_writer->save('php://output');
Is there a way to fix this issue?

Unable to download a file using PHPexcel

I am using the following code to download a excel file
$objPHPExcel = new PHPExcel();
after this I add data to the Excel object and finally I call the code
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=$exportFileName');
$objWriter->save('php://output');
die();
Here $exportFileName is the name of the .xlsx file But when I execute the code , the file does not downloads but I can see the following junk characters in the console
Let me know How to resolve this issue?

Warning after download and open Excel file exported from Magento

I using exporting data to excel 2007 using PhpExcel 1.8.0. Its working but
Excel File is saved in Client side but when I open the downloaded Excel file, I get the warning message
We found a problem with some content in 'filename.xlsx'. Do you want
us to try to recover as much as we can? If you trust the source of
this workbook, click Yes.
I am using PhpExcel library in Magento.
Could anyone help me to solve this problem?
Code is below:
$objPHPExcel->getActiveSheet()->setTitle($store_name." Product List");
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=".Mage::getBaseDir().'/media/productpricinglist/'.$store_name.'product_list.xlsx');
header("Cache-Control: max-age=2");
ob_clean();
$objWriter->save("php://output");
I had this problem too. I just added exit; at the end of my PHP script

Issue with PHPExcel 1.7.9, 2013-06-02 [duplicate]

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');
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
ob_end_clean();
ob_start();
It cleans the buffer and get correct output. At least for me it resolve the issue.

PHPExcel save file in a folder or open excel

I am using PHPExcel to export data from my PHP page to Excel.
I am using Excel5.
I want the excel file to be be saved a particular folder that is specified in the code
OR better still,
I want Excel to open with the data written in it so that user can save it wherever he wants.
What should I do.
Please guide me
Pre
I solved this problem doing this:
$objWriter->save(str_replace(__FILE__,'/path/to/save/filename.extension',__FILE__));
In my case, it worked!
This will ask the user to save / open the file:
$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// ...
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5);
$writer->save('php://output');
Look at 01simple-download-xls.php in the PHPExcel Tests directory. This sends the Excel file to the user's browser, which then prompts them to either display it (in Excel if they have it installed, or other spreadsheet program if they have the extension associated with LibreOffice Calc or Gnumeric or whatever), or save it to their local disk.
This solved this issue for me:
$this->objWriter->save(str_replace(__FILE__,$_SERVER['DOCUMENT_ROOT'] .'/path/to/save/filename.xlsx',__FILE__));

Categories