MIME-Type for Excel XML (ASP.NET 3.5)

I use CarlosAG-Dll which creates a XML-Excel-file for me (inside a MemoryStream).

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "myfile.xml");
memory.WriteTo(Response.OutputStream);

My Problem here is, that I get at client side a myfile.xls (IE) or a myfile.xml.xls (FF) and therefore get an annoying security warning from excel.

I tried it as well with application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx) but then it won’t even open.

So I need to either cut the .xml and send it as vnd.ms-excel (how?) or take another MIME-type (but which one?).


edit: I found a bug description here

I wonder if this is still open and why?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use like this

Response.ContentType = "application/vnd.ms-excel";

Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");

For Excel 2007 and above the MIME type differs

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Response.AppendHeader("content-disposition", "attachment; filename=myfile.xlsx");

See list of MIME types

Office 2007 File Format MIME Types

EDIT:

If the content is not a native Excel file format, but is instead a
text based format (such as CSV, TXT, XML), then the web site can add
the following HTTP header to their GET response to tell IE to use an
alternate name, and in the name you can set the extension to the right
content type:

Response.AddHeader "Content-Disposition", "Attachment;Filename=myfile.csv"

For more details see this link

Method 2

If your document is an Excel Xml 2003 document, you should use the text/xml content type.

Response.ContentType = "text/xml";

Do not specifiy content-disposition.

This technichs works great with Handler, not with WebForm.

Method 3

The security warning is NOT about the MIME type – it is a client-side security setting you can’t disable from the server side !

Another point – change Response.AppendHeader("content-disposition", "myfile.xml"); to:

Response.AppendHeader("content-disposition", "attachment; filename=myfile.xlsx");

OR

Response.AppendHeader("content-disposition", "inline; filename=myfile.xlsx");

For reference see http://www.ietf.org/rfc/rfc2183.txt

EDIT – as per comment:

IF the format is not XLSX (Excel 2007 and up) then use myfile.xls in the above code.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x