1

We have a working PHP code snippets for ages. We are generates simple HTML tables and than the user can save these as an XLS file. And open it in Excel.

Now when we open this kind of files Excel opens and display a totaly empty grey area. No error, no message. Just a blank space. It is only occures on machines where the Office 2010 got the last (2016.07.14) official Microsoft Office updates!

The above mentioned PHP snippet is very easy:

header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-Disposition: attachment; filename=CO3_finance_export.xls");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $out);

BOM is there, and $out contains the table. If you put any simple table in the $out - it is not woking. For example:

<table><tr><td>XX</td></tr></table>

It is very strange since if I open the generated file in Notepad++ and save it again (nothing changed), than Excel will open it correctly.

I think the problem is somewhere with iconv... since I remove it Excel display some weird characters.

Are there somebody in the same situation?

I would appreciate any help or suggestion about my problem.

Tamas
  • 33
  • 5
  • Did you compared the file content before Notepad++ and after with a Hexeditor? I have frequently similar problems when opening MS Access .mdb files from a network share. I double click it and nothing happens. If I start manually M$A, and go to File -> Open it works. Maybe its a security issue: Try saving the downloaded file on a FAT volume (USB-stick). Windows is unable to store ADS (AlternateDataStreams) with a ZoneIdentifier (This File comes from the evil Internet) on FAT. – user5329483 Jul 25 '16 at 19:00
  • Thank you, I will try the hexeditor. I only did a file compare between the wrong and a good file, but it said the files are the same. – Tamas Jul 25 '16 at 19:16
  • OK, I have checked the wrong and the good file with HEX editor. It turned out that in the wrong file, all off the characters are on two bytes. Forg eg. 3c 00 means <. But I think it is OK, since UTF-16 needs two bytes. In the good file all of the characters are on one byte.... And this file is good for Excel now. I'm confused..... :( – Tamas Jul 25 '16 at 19:33
  • Obviously `iconv()` did it's converter job. What's happen if you feed Excel directly with $out? Maybe the extension`.xlsx` alias mime `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` would be an approach. Just a few ideas... – user5329483 Jul 25 '16 at 20:07
  • When if I feed Excel just with $out without BOM or with BOM but without iconv I get unformatted chinees like characters. The .xlsx and the xml format header not working. – Tamas Jul 25 '16 at 20:53

0 Answers0