PHPExcel output file is empty - phpexcel

I downloaded PHPExcel library but could not use it. I am using one of the examples in the Library but it downloads an empty excel file. Here is my code.
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("document");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'world!');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->setActiveSheetIndex(0);
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment;filename="deneme.xls"');
Am i forgetting something here? It is simple but downloaded excel file is empty. Does anyone have an idea?
EDIT:
Solved: I have to add these lines:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

Add this:
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
die();

You're forgetting to instantiate a writer and then save

Related

ZipArchive::close(): Renaming temporary file failed: Invalid argument Phpexcel Laravel

I am working on append excel content into existing excel file and save it in a predefined directory with different name. I am using the Laravel 5.4 framework for my application.
I am using the below code to create excel file and save it on specified location:
$objPHPExcel = \PHPExcel_IOFactory::createReader('Excel2007');
//Load Existing excel file into I want to append
$objPHPExcel = $objPHPExcel->load(getDocumentPath().'/Report_Template_UG.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
//Here I have Written code to insert my data into loaded excel file
$today = date('d-m-Y');
$time = date('H:i:s');
$filename = 'Report_'.$today.' '.$time.'.xlsx';
//welcomed is the root directory name of laravel project
$dir = 'C:\xampp\htdocs\welcomed\storage\documents\';
$filepath = $dir . $filename;
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($path);
On executing the above code, I am gettign the below error:
ErrorException in Excel2007.php line 377:
ZipArchive::close(): Renaming temporary file failed: Invalid argument
Can anyone please tell me, What I have done wrong in above code or do I have missed something?
Thanks

While exporting to XLS output is garbage even with correct headertypes

I have tried some solutions provided in other questions about exporting to Excel sheet using PHP,still not working.Also I referred to PHPExcel library examples still stuck.
Here's my code.
<?php
ob_start();
require_once 'Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties() ->setCreator("MeritTrac")
->setTitle("PHPExcel Test document")
->setSubject("PHPExcel Subject")
->setDescription("This is generatred by PHPExcel");
$objPHPExcel ->setActiveSheetIndex(0)
->setCellValue('A1','Hello')
->setCellValue('B1','Test')
->setCellValue('C1','Color')
->setCellValue('D1','World');
$objPHPExcel ->setActiveSheetIndex(0)
->setCellValue('A4','Miscellaneous Glyph')
->setCellValue('A5','ífá');
$objPHPExcel ->setActiveSheetIndex(0);
header('Content type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Content type:application/vnd.ms-excel');
header('Content disposition: csv;filename="download.xlsx"');
header('Cache-Control: max-age=0');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public');
echo "Test print";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>
<?php
ob_end_flush();
?>
Output:
http://imgur.com/a/5AsWF
Please find what I am missing coz this code gives me garbage output.

Excel 2010 cannot open certain size xls files created by PHPExcel

We have been using PHPExcel in our production environment for over 4 years now and it has been working great without any problem until last week when Excel 2010 & 2103 was not able to open one of many xls files generated by PHPExcel. The error which we got was
Error: "Excel found unreadable content in file. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes." .
We tried to repair and step through other suggestions we found but none worked.
After generating many files we were at least able to reproduce this issue consistently. Though it seems very strange but Excel 2010 & 2013 running on windows 7 & 10 is not able to open any files generated by phpExcel that are between 6914K - 6977K file size.
We cannot find any solution or reason why Excel 2010/2013 would not open these files. Are we missing something? Thanks in advance for any suggestions.
We are using...
PHPExcel: 1.8
Webserver: Apache/2.2.15 (Unix) & PHP 5.3.3 running on Red Hat
Sample code to reproduce the issue.
require_once dirname(dirname(__FILE__)) . '/Classes/PHPExcel.php';
$timestamp = date("YmdHis");
$filename = "report_" . $timestamp . ".xls";
$rowcount = 20150;
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$objPHPExcel = new PHPExcel();
$VAR1 = '99999';
$VAR2 = 'XXX';
$VAR3 = 'XXXX';
$VAR4 = 'XXXXX';
$VAR5 = 'XXX';
$VAR6 = 'XXXXXXXXXXXXXXXXXXXX';
$VAR7 = 'XXX';
$VAR8 = 'XXXXXXXXXXXXXXXXXXX';
$VAR9 = 'XXXXXXXXXXXXXXXXXXX';
$VAR10 = '999999999';
$VAR11 = 'xxxxxxxxxxx';
$VAR12 = 'XXXXXXX';
$VAR13 = 'XXXX';
$VAR14 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$VAR15 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$VAR16 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$VAR17 = 'XXX';
$VAR18 = '99999';
$VAR19 = '9999999999';
$VAR20 = '999999999';
$VAR21 = 'XXXXXXXXXXXXXXXXXX';
$VAR22 = '99999';
$VAR23 = '999999';
for ( $i=2 ; $i<intval($rowcount) ; $i++ )
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$i", "$VAR1")
->setCellValue("B$i", "$VAR2")
->setCellValue("C$i", "$VAR3")
->setCellValue("D$i", "$VAR4")
->setCellValue("E$i", "$VAR5")
->setCellValue("F$i", "$VAR6")
->setCellValue("G$i", "$VAR7")
->setCellValue("H$i", "$VAR8")
->setCellValue("I$i", "$VAR9")
->setCellValue("J$i", "$VAR10")
->setCellValue("K$i", "$VAR11")
->setCellValue("L$i", "$VAR12")
->setCellValue("M$i", "$VAR13")
->setCellValue("N$i", "$VAR14")
->setCellValue("O$i", "$VAR15")
->setCellValue("P$i", "$VAR16")
->setCellValue("Q$i", "$VAR17")
->setCellValue("R$i", "$VAR18")
->setCellValue("S$i", "$VAR19")
->setCellValue("T$i", "$VAR20")
->setCellValue("U$i", "$VAR21")
->setCellValue("V$i", "$VAR22")
->setCellValue("W$i", "$VAR23");
}
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("J")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("K")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("L")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("M")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("N")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("O")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("P")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("Q")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("R")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("S")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("T")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("U")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("V")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("W")->setAutoSize(true);
ini_set("max_execution_time","900");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("reports/$filename");
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
unset($objWriter);
echo "<a href=reports/$filename>Download this file</a><br><br>";
Try to change this:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
To this:
$objWriter = new PHPExcel_Writer_Excel2007 ( $objPHPExcel );
And change the extension .xls to .xlsx

Don't print the chart in pdf with phpexcel

Currently I'm making my reports with phpexcel, everythink is ok when I make the report with charts in xlsx;
... $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); $objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');
exit;
and when I do it in html with;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');
But the problem is when I want to create the pdf file my response is a pdf without charts, and yes previously I added the PHPExcel_Settings::PDF_RENDERER_MPDF AND PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');
May be there is a problem because I save the file in the client browser?

PHPExcel File format or extension is not valid

I'm using phpexcel for export my query in excel file; however after I created file(which is xslx format), I can not open my file in excel. It gives "the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the file format of the file" error. When I open the file in texteditor(mine is npp) I see my php file's css codes and some part of my html codes. My code is like that;
if( ! empty($_POST['export'])){
$query = "SELECT * FROM asd ORDER BY asdf LIMIT 10";
$headings = array('Timestamp', 'h1','h2');
require 'Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Users');
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
$rowNumber = 2;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
$objPHPExcel->getActiveSheet()->freezePane('A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xlsx"');
header("Content-Transfer-Encoding: binary ");
//ob_end_clean();
//header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
I'm stucked please help. Thank you.
As described in the manual.... if anything else is being output to the browser, this will corrupt the output file.
Open the file in a text editor, and look for leading or trailing whitespace characters (spaces, tabs, newlines) or a BOM marker at the beginning of the output, or for any obvious PHP plaintext error messages in the content. These are the most obvious causes of this problem. Once you've identified the spurious characters, check through your script to see where that output is being generated, and remove it.
In your case, that means don't output your css and html.
EDIT
xlsx is the extension for an OfficeOpenXML Excel2007 file, not for a BIFF 8 xls file.... be consistent in your headers (mime type and file extension) and Writer
Either:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');
or
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="userList.xlsx"');
Following documentation or comments somewhere I read, I had this:
$objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
// etc
instead of
$objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// etc
And that caused Firefox and IE/Edge to append an .xls to the filename and also give the error message when trying to open the file directly instead of downloading it. When downloading the file and opening the spreadsheet though, everything was fine.

Resources