PHPExcel generated an excel file that does not work. (File format or extension is invalid)

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

The PHPExcel sample code runs successfully on my local PC, the file is downloaded to the local file and opens successfully in Excel 2007. But using the same code on my server gives an invalid file for Excel, which, when I OPEN AND REPAIR, then opens successfully.

I do not want to use the Open and Repair option, but only the Open option.

Could not find a 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; 
+4
source share
6 answers

The file you posted has a single space before exiting PHPExcel ... check your script to see where it can be sent to the php: // output stream. Make sure there is no space in front of the original <?php tag; pay attention in particular to ?> <?php or similar closing / opening tags. Also check included files with script

+15
source

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

The code looks like this: // 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; 
+20
source

Check for any false characters in the file by opening it in a text editor. If so, you can probably figure out where they are being administered. Spaces or line breaks are the obvious culprits.

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

The xlsx format is actually a zipped collection of xml files, so if the text editor does not have visible text strings (other than a PK signature), try unzipping them. This may give other clues.

If you are running on the Windows platform, then there is some error in the ZipArchive php_zip.dll reference library that may cause this error. You can use PCLZip as an alternative to ZipArchive.

+5
source

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'); 
+3
source

Writing an Excel file is not just providing an arbitrary xlsx or xls extension file.

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

For more information and an example with Excel 2007, you can refer to the PHPExcel pages .

Let me know if I can help you more.

+1
source

I ran into the same problem and found that a new string character appeared that started at the beginning of the desired data and caused this error. Later I discovered that I have a php file with a closing tag

 "?>" 

It is generally recommended that you avoid using the closing php tag, as they also go to the output buffer. Using ob_end_clean () is also a good option.

More information can be found. Why omit the close tag?

0
source

Source: https://habr.com/ru/post/1215622/


All Articles