How to convert xls to xlsx in codeigniter - php

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

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?

how to save multiple files using phpexcel

i want to save 6 files which are in loop but only first file is getting saved.
code as below :
$dte=date('d-m-Y',strtotime($dt));
$fname='Picksheet'.$dte.'_Zone_' .$zone.'.xls';
$filename='Backup.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$fname.'"'); //tell browser what's the file name`enter code here`
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');

PHPExcel saving HTML content to xls/x file

I have this code:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'test');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$objWriter->save('php://output');
I have 2 problems.
Any headers I tried to save the file with .xlsx extension didn't worked. they all gave me a corrupted file.
My second problem which is much more important - this code worked with .xls extension.
But it download a file with all of the HTML content of the page and when I opened it Excell is giving me an error that I missing the css file.
I'm using this code on the page index.php with include(), the css file is called in index.php.
any help would be appreciated, thx!
i used this code in 4-5 projects and working perfectly, Check if helps
$objPHPExcel->getActiveSheet()->setTitle('Customers');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=Customers-"' . date('d-m-y-h-i-s', time()));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

PHP Excel: Saving created PDF to remote server and not user downlaod

This is similar to my previous question re (DOMPDF), the difference is that now i am using PHPExcel, to create the PDF.
I have tried the file_put method but it downloads an empty PDF. SO where am i going wrong or can this not be done?
below is my end code that outputs the fiel and what i have tried
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="TestSheet.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
$file_location = $save_Path;
file_put_contents($file_location,$objWriter);
exit;
It seems that once its been saved its clearing the data so hence the blank PDF
Thanks
Discard the headers.... they're all about telling the web browser to expect a PDF file, but you're not sending anything back to the browser (let alone a PDF file).
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
is telling PHPExcel to send the generated PDF directly to the web browser.... you say you don't want to do that, but to save to a remote server instead.
You'll need to intercept the results from $objWriter->save() so it doesn't go to the browser. Capture it using output buffering:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
ob_start();
$objWriter->save('php://output');
$myPdfData = ob_get_contents();
ob_end_clean();
Then you've captured the PDF datastream in $myPdfData and can then do:
file_put_contents($file_location, $myPdfData);
Note that this is likely to be memory-hungry

Categories