Getting Error "Excel cannot open the file '....xlsx' because the file format or file extension is not valid."

Topics: Developer Forum
Mar 28, 2013 at 3:55 PM
I have used PHPExcel quite a bit. I hadn't worked with it for a while until recently when I revised an earlier file. As far as I can tell I have everything set up right, but I am getting the above error when I try to open the file in Excel and can't tell what the problem is. After closing the error I can open the file in Notepad++ and it shows the usual Excel gibberish. Doing that I was able to debug problems with the data, but now that those are handled, still cannot open the file.

Here's my code for setting the properties and writing the file:
protected function set_excel_properties(){
        $title = "DNCS Video Upstream Capacity Report";
        $this->vce->getProperties()->setCreator("D Newey")
                                     ->setLastModifiedBy("D Newey")
                                     ->setTitle($title)
                                     ->setSubject($title)
                                     ->setDescription("Office 2007 XLSX document for ". $this->last_we)
                                     ->setKeywords("office 2007 openxml php capacity " . $this->last_we)
                                     ->setCategory("DNCS Capacity " . $this->last_we);  
        $this->vce->setActiveSheetIndex(0);                              
        }

public function write_excel(){
        $this->vce->objWriter = PHPExcel_IOFactory::createWriter($this->vce, 'Excel2007');
        $this->vce->objWriter->save('php://output');
        }
I seem to recall there being some particular errors that caused that error message to display when trying to open the file in Excel, but I haven't been able to find anything on the site here. If you need anything else let me know.
Coordinator
Mar 28, 2013 at 4:25 PM
The normal circumstance is that your code is echoing something to php://output (whitespace, BOM header, or error messages being the most common) that can be seen in a text editor; especially if save to disk works and save to php://output doesn't. Failing that, it requires detailed examination of the code an file to identify the problem
Mar 28, 2013 at 5:04 PM
Yes, I was able to locate error messages using the text editor and made sure those were all handled. The last download I tried to create looks like it has white space at the beginning and end. How do I prevent that? Also, how would I recognize a BOM header? Thanks.
Coordinator
Mar 28, 2013 at 10:42 PM
If there's any open/close PHP tags such as <?php and ?> within your script, they can give whitespace; Your IDE/Editor will normally allow you to save a file as UTF-8 with or without BOM markers, ensure that you've saved without... the UTF-8 BOM is \xEF\xBB\xBF
Mar 29, 2013 at 4:16 PM
All I have are the open and close tags at the beginning and end of the file. I'm not sure I understand what you're saying about the IDE/Editor saving "the file as UTF-8 with or without BOM markers." How does this apply to generating an Excel Download from a webpage? Are you saying that BOM markers in the code file would cause it to not generate a clean xlsx file?


Coordinator
Mar 29, 2013 at 7:47 PM
I am saying exactly that. Anything that is output by the script to a browser (ie to php://output), other than the actual output generated by executing the save() will corrupt the file because it will be included as part of the file. Even a trailing newline character after a closing ?> can corrupt a file: that's why none of the PHPExcel scripts themselves have any closing ?>
Mar 29, 2013 at 8:00 PM
Ok, I guess I'll have to just keep combing through it. The file is writing utf-8 and not creating BOM markers as near as I can tell. It appears to create extra white space, but for the life of me I can't tell where. I don't have a trailing ?> tag. When I open up the file in Notepad++ white space appears at the top. I've tried removing that and resaving, but that still doesn't work, so I'm at a loss - I'll keep combing.


Apr 10, 2013 at 9:46 PM
Mark,

I wanted to let you know that it was a problem with white space which I finally located at the bottom of the extended class. It works fine now.

- David




Apr 23, 2013 at 10:12 AM
Checkout these reference would be help you:

http://social.technet.microsoft.com/Forums/en-US/excel/thread/51bde1c2-ab35-4716-afa9-8e3207b2d7f5/

http://in.answers.yahoo.com/question/index?qid=20091222225206AA9K9uN

If the above solution did not help you then you can try this third party utility from here:

http://www.recoverexcel.org


Thanks
Apr 12, 2015 at 7:43 PM
Hi,

Just wanted to add solution for my recent problem in PHPExcel.
I got error while opening excel because I used scandic in "Set document properties".
I took me over an hour to solve this issue, but when I removed those 'illegal' letters from properties, everything worked fine again