tags:

views:

773

answers:

2

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!

+2  A: 

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.
Doug
When I use .xlsx extension, I get a different error message: Excel cannot open the file blah.xlsx because the file or format or file extension is not valid... without any options to open the file at all
laconicdev
The workbook in this comment:http://stackoverflow.com/questions/150339/generating-an-excel-file-in-asp-net/150368#150368works correctly for me with an xlsx extension. Is it possible that the file you're trying to open has a mismatched tag or other syntax error?
Doug
A: 

You may want to check out this question it has some good links and answers:

http://stackoverflow.com/questions/652377/asp-net-excel-spreadsheet-generation-results-in-different-file-format-than-ext

BrianP