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>
php phpexcel
fred2
source share