views:

1335

answers:

5

I've been looking for a good Excel file generation class and have yet to find one - my number one issue is that I although I can get the exported file to open in Excel (running 2007), I always get a warning saying "the file is in a different format to the file extension".

I noticed that the Excel export in phpMyAdmin even generates the aforementioned warning message.

What code do other users use for this common requirement?

Quick update If I set the file extension of my generated Excel file as xml it opens in the browser (even though I'm sending the correct headers), but if I choose to save the xml file and then open it from Excel, it works without any warnings! Unfortunately, that's more hassle for the users than simply clicking 'Proceed anyway' with the warning I'm getting. Any more ideas?

+2  A: 

Use Spreadsheet_Excel_Writer from PEAR

or

Write the file to CSV format.
But use Microsoft Excels understanding of csv: A semicolon(;) as seperator

Bob Fanger
I've taken a look at the PEAR package, it looks like a lot more work to implement - plus I'm not sure it will resolve my issue, which does seem to effect only Excel 2007.
BrynJ
Don't use the PEAR package, it's outdated! It has been in beta since 2006. Use PHPExcel instead!!!
tharkun
A: 

the file is in a different format that the file extension

Have you tried saving the file with another file extension? Like "foo.xlsx" instead of "foo.xls".

Ionuț G. Stan
I have, yes - xls, xlsx and xml - all with the same result. I guess it is malformed in some way, but I'm not sure exactly what.
BrynJ
Ok, just to add to the above - in actual fact, if I set the file extension as xml it opens in the browser (even though I'm sending the correct headers). If I choose to save the xml file and then open it from Excel, it works without any warnings! Unfortunately, that's more hassle for the users than simply clicking 'Proceed anyway' with the warning I'm getting.
BrynJ
+2  A: 

You also disable the dialogbox, using regedit

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security]
“ExtensionHardening”=dword:00000000
  • Start -> Run -> regedit
  • Go to "HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY"
  • New DWORD
  • Type “ExtensionHardening” as the name and 0 as the value
Bob Fanger
Interesting, but not really a viable solution for a webbased app though +1 anyway.
Toby Allen
+1 - I may be able to use this actually, as I'm providing this functionality in the administration area of my web app, so the users are finite (it's a bespoke app).
BrynJ
+1  A: 

I've discovered that adding the following, under the xml declaration in my generated Excel file, ensures Excel opens the file with an xml file extension (rather than the web browser):

 <?mso-application progid=\"Excel.Sheet\"?>

I'm also setting the following headers in php:

// deliver header (as recommended in php manual)
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '.xml"');
BrynJ
+3  A: 

http://www.phpexcel.net

Thanks for the link - it looks like a very comprehensive class, to be honest it's far in excess of my requirements.
BrynJ
You shouldn't be afraid of the size when the quality is good. Much better than using the outdated PEAR class.
tharkun