Unable to download a file using PHPexcel - php

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?

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?

How to convert xls to xlsx in codeigniter

I am using PHPExcel for downloading the file as xlsx format. I am using codeigniter framework. Previously i am downloading xls format. At that time the file is downloaded successfully. If i am trying for downloading as xlsx format it gives the error as below image.enter image description here
My code is :
$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');
There is no error when i am adding as xls format. It gives the error when i am adding as filename.xlsx. Any solution please.
you may continue with php Output buffers and codeigniter download helper
for example :-
$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);

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

Error while opening an Excel file that generated using phpExcel

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.

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.

Categories