Home » Php » php – PHPExcel generated excel file not working. (File format or extension is invalid )

php – PHPExcel generated excel file not working. (File format or extension is invalid )

Posted by: admin July 12, 2020 Leave a comment

Questions:

PHPExcel generated excel file not working. (File format or extension is invalid)

The example code of PHPExcel exceutes successfully on my local PC, the file gets downloaded to local and is opened in Excel 2007 successfully.
But using the same code on my server gives an Invalid file to excel, which when I OPEN AND REPAIR, then it opens successfully.

I don’t want to use the Open and Repair option, but only the Open option.

Couldn’t find out the problem? Please help.

$objPHPExcel->setActiveSheetIndex(0);

//for XLSX
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

exit;
How to&Answers:

The file that you’ve posted has a single space character before the PHPExcel output… check your script to see where this might be sent to the php://output stream. Check that there’s no space before your initial <?php opening tag; watch out in particular for ?> <?php or similar closing/opening tags. And also check any files that might be included by your script

Answer:

Add a line ob_end_clean(); at the end of your program before the line $objWriter->save('php://output');

The code looks like:
//for XLSX

$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');


header('Content-Disposition: attachment;filename="item_list.xlsx"');

header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

ob_end_clean();
$objWriter->save('php://output');


exit;

Answer:

Check for any spurious characters being echoed into the file by opening it in a text editor. If there are, you can probably figure out where they’re being introduced. Spaces or line breaks are obvious culprits.

Also, make sure that there are no spaces or newlines before the <?php tag or following the ?> tag in any of your files.

The xlsx format is actually a zipped collection of xml files, so if there are no obvious text strings (beside the PK signature) visible in a text editor, then try unzipping it. That may give other clues to the issue.

If you’re running on a Windows platform, then there have been some buggy versions of the ZipArchive php_zip.dll link library that can cause this error. You can use PCLZip as an alternative to ZipArchive.

Answer:

IS application/vnd.ms-excel

AND NOT

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

EXAMPLE FOR XLS:

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

EXAMPLE FOR XLSX:

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

Answer:

Writing an Excel file isn’t simply a matter of giving an arbitrary file an extension of xlsx or xls.

You can write a BIFF5 or BIFF8 xls file (all versions of Excel post 95 will read this), or there is the Excel 2003 XML format, which can be opened by both Excel 2003 and Excel2007 (and Excel 2010 as well).

For more detail and example to do with excel 2007 you can refer to the PHPExcel examples page.

Let me know if I can help you more.

Answer:

I faced same issue and found there was a new line character, which was coming from the beginning of the desired data and was causing this error. Later I found that I had a php file which had closing tag

"?>" 

Usually it is recommended that one should avoid using closing php tag as they also go to output buffer. Using ob_end_clean() is also good option.

More details can be found Why would one omit the close tag?

Answer:

I solved my problem, I installed zip using command

sudo apt-get install php7.0-zip
and Restart the server
sudo service apache2 restart
It works for me
I used https://github.com/PHPOffice/PhpSpreadsheet