Messy file with Excel2007

Topics: Developer Forum
Apr 5, 2013 at 10:44 PM
Hi, I'm using PHPExcel with Yii framework. I need to export data to Excel 2007 but I get a messy file, full of chinese characters.
The PHPExcel version: 1.7.8
The Yii version: 1.1.6
My 'save' code:
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save(str_replace('.ext', '.xlsx', 'c:\export.ext'));

But saving to excel5 is ok:
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
I just can't use Excel5 because I need images within the report's header, and Excel5 seems not to suport it.
If someone can say something about.. give an idea.. very thanks.
Apr 8, 2013 at 12:05 PM
Please provide some example of a messy file, full of chinese characters as it's not particularly descriptive. You shouldn't be seeing chinese characters; but a binary file could easily be construed as messy, and an xlsx file is a binary file. How are you opening the resulting file to see it is a mess of characters? Does MS Excel throws an error when reading it?
Apr 8, 2013 at 3:42 PM
Yes, the characters actually aren't the chinese ones. And, yes, MS Excel throws errors when I try to open the resulting file. I tried in two ways. Here go the steps I followed, the messages and the results:

By opening the .xlsx file in Excel 2007 I get the following message:
"Excel found unreadable content in 'report_file.xlsx'. Want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
By clicking 'Yes' I get the next message:
"The file is corrupt and can not be opened."
And then the file is not opened.

So I renamed the resulting file extension to .xls ("report_file.xls") and tried open in Excel 2007, getting the following message:
"The file you are trying to open, 'report_file.xls', is in a different format than specified by the file extension. Make sure the file is not corrupted and is from a trusted source before you open it. Want to open the file now?"
By clicking 'Yes' the file is opened and I see what actually isn't chinese chars, like the folowing, copy and pasted:

on cell A1:
on cell A2
on cell A3
on cell A17
and so on.
That is what actually happens.
Apr 8, 2013 at 4:11 PM
Simply renaming the file shouldn't work: BIFF (.xls) and OfficeOpenXML (.xlsx) are two totally different formats.

It's likely that your script is outputting something that gets injected into the file.

Open the file in a text editor, and look for leading or trailing Whitespace characters (or a BOM marker as the first 3 characters), or any obvious PHP error messages or the result of any echoes/prints in your code in plaintext in the file