Save as with Excel2007

Topics: Developer Forum, User Forum
Jan 21, 2009 at 2:02 PM
I cannot get my worksheet to open when I attempt to download it through the browser. I am passing the download headers, saving to disk and using fpassthru to try to send it to the user. The version I pull directly off the server gets the unreadable content errors, but it does open. The version that I save or open through the browser gives me an invalid file format error and will not open. any ideas?
Jan 21, 2009 at 2:13 PM
Try to make it work first where you save ordinarily to disk on server (not sending to client browser). Sending file to browser there are many more pitfalls.

If you get corrupt xlsx file saving ordinarily to disk it could be a defective zip extension, or it could be your coding. There is a thread about defective zip extension:

Jan 21, 2009 at 2:24 PM
When I save directly to the server the only error I get with the file is an unreadable content error. The file opens and cleans up fine when I get the file from the server.  I am on a Unix/Apache server if that helps anything.

Basic server info
  PHP version: 5.2.6
  Zip version: 1.8.11
Jan 21, 2009 at 2:41 PM
Ugh... if you are getting "unreadable content error" from MS Office Excel then something is wrong and needs to be fixed.

Does this happen for all files you generate? Can you try to change extension from xlsx to zip and see if the file can be unzipped without any errors?

Jan 21, 2009 at 2:52 PM
What is it supposed to look like when it is zipped? I extract and it just dumps three folders and an xml file into my destination that I choose.
Jan 21, 2009 at 2:56 PM
And when I just make a quick document similar to example 1 I don't get an unreadable content error, so I'm guessing that it is a problem with my code
Jan 21, 2009 at 3:00 PM
Yeah, then it's probably your code. Try to remove statements and isolate the problem. For example, you can get corrupt xlsx file if you are not using UTF-8.

Jan 21, 2009 at 3:52 PM
Ok I fixed the part where it was saving to the server with the unreadable content, now when I download it, it will open and get the unreadable content error
Jan 21, 2009 at 4:05 PM
In that case you may have missed the guidelines from the documentation when sending files directly to browser:
  • Make sure not to include any echo statements or output any other contents than the Excel file. There should be no whitespace before the opening <?php tag and at most one line break after the closing ?> tag (which can also be omitted to avoid problems).
  • Make sure that your script is saved without a BOM (Byte-order mark). (Because this counts as echoing output)
  • Same things apply to all included files
If you think you are doing everything right, compare downloaded file with file saved on server disk. They should look the same in a HEX editor. Try to find out how they differ. Usually there is prepended or appended some stray characters (may even be just white space) because one or more of the above guidelines are violated.