.net – Warning message when opening a generated excel file in office 07

excelnetxml

I generate an excel file using XML format outlined on stackoverflow in the following post:
Generate excel file in .net

However, when I open it up in excel 07, I get the following message:

The file you are trying to open,
"blah.xls", is in a different format
than spefied by the file extension.
Verify that the file i snot corrupted
and is from a trusted source before
opening the file. Do you want to open
the file now?

What is causing the error message and how can I get rid of it? Thanks!

Best Answer

Try calling the file blah.xlsx instead of blah.xls. Excel apparently wants XML files to have an "xlsx" extension. The "xls" extension is for the binary format files.

Edit in response to your comment:
I was wrong: xlsx files aren't just the XML files, they're zip archives containing the XML format plus other metadata. All in all, it's a little complex to set up. I'd try renaming the file to blah.xml, and see if that works. Otherwise I'm afraid you might have to look at how to make these zip files. There are two options:

  • Use Microsoft's OOXML SDK: see http://msdn.microsoft.com/en-us/library/bb448854.aspx.
  • Do it yourself (much harder). You'll have to look at the OOXML standard. Part 1 contains an overview, and a description of each XML file. Part 2 describes the package format (i.e. the zip archive format). There are additional requirements on xlsx files above what part 2 says you need, so read part 2 first, then part 1.
Related Topic