PHPExcel creates "unreadable content"

I tried to fix this problem in about 1000 different ways. Appreciate if anyone else can spot the problem.

I have code using PHPExcel that generates multiple Excel worksheets and saves them to disk.

When you open everything from the second file using MS Excel 2010, I get the error message "Excel found unreadable content in FILENAME. You want to restore the contents of this book." The file is "restored" and works fine. Opening in OPEN Office does not cause errors.

Here is a simplified version of the code.

<?php $filenames = array("filenames go here"); $sheet1 = PHPExcel_IOFactory::load($template1); $writer1 = new PHPExcel_Writer_Excel2007($sheet1); $writer1->save('somefilename.xlsx'); //This file opens without problem $sheet1->disconnectWorksheets(); unset($sheet1); unset($writer1); $i = 0; foreach($filenames as $file){ $template = "template2.xlsx"; $fileName = 'filename' . $i . ' .xlsx'; $spreadsheet = PHPExcel_IOFactory::load($template); $objWriter = new PHPExcel_Writer_Excel2007($spreadsheet); $objWriter->save($fileName); $spreadsheet->disconnectWorksheets(); unset($spreadsheet); //This file throws error when opened in Excel 2007+ $i++; } ?> 

I checked the following things:

  • There are no obvious php error messages, spaces or extraneous inputs that should distort the file.
  • The template file opens fine in Excel 2010.
  • I tried using different PHPExcel writing methods, but they all pose the same problem.

The real code does a whole load of additional material, but I checked that it is not responding by commenting on it. The above code is the only place where the problem can be posed.

Any suggestions gratefully received.

==== ==== EDITED This is the error log created by Excel:

 <?xml version="1.0" encoding="UTF-8" standalone="true"?> -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <logFileName>error049120_01.xml</logFileName> <summary>Errors were detected in file 'C:\Users\USERNAME\AppData\Local\Temp\FILENAME.xlsx'</summary> -<additionalInfo> <info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info> </additionalInfo> </recoveryLog> 
+1
php phpexcel
source share
5 answers

This error was related to the configuration of mbstring.func_overload in php.ini. You can use a workaround to avoid this error - just add at the beginning of your script:

 if (function_exists('mb_internal_encoding')) { $oldEncoding=mb_internal_encoding(); mb_internal_encoding('latin1'); } 

and in the end:

 if (function_exists('mb_internal_encoding')) mb_internal_encoding($oldEncoding); 

It helped me, so I think it will help too.

+4
source share

I had a similar error in my file and I received a recommendation published by Andrei Yanko, however, I still get similar errors. I found suggestions for another post and adding

"exit";

at the end of my script that helped remove the remaining errors

+7
source share

The obvious problems are:

 $sheet1 = PHPExcel_IOFactory::load($template1); $writer1 = new PHPExcel_Writer_Excel2007($sheet1); $writer1->save(sheet1); 

save($sheet1) - $ sheet1 is not a file name, it is a PHPExcel object, a complete book ... the save method expects the name of the file that you want to save as an argument

 foreach($filenames as $file){ $template = "template2.xlsx"; $fileName = 'filename.xlsx'; $spreadsheet = PHPExcel_IOFactory::load($template); $objWriter = new PHPExcel_Writer_Excel2007($spreadsheet); $objWriter->save($fileName); $spreadsheet->disconnectWorksheets(); unset($spreadsheet); } 

Each loop saves the same file name, overwriting any saved previous iterations

If MS Excel complains about unreadable content but still opens the file, it will usually tell you what was dropped to make the file readable ... but without knowing this message or not seeing a copy of the actual file, it is impossible to help you.

+2
source share

Maybe you save it as .xls not .xlsx ?

Opening a new .xlsx , renamed to .xls , gives me a similar warning.

+1
source share

It seems that this error was related to a separate xlsx file, which I used as a "template". The code works fine when another file is used instead.

The original template did not seem to be corrupted, so somewhere near PHPExcel there was a problem, but you don’t know where.

0
source share

All Articles