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

41,837

Solution 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

Solution 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.

Share:
41,837
UNeverNo
Author by

UNeverNo

Quotes I like: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilisation. A good programmer is someone who looks both ways before crossing a one-way street. The most likely way for the world to be destroyed, most experts agree, is by accident. That’s where we come in; we’re computer professionals. We cause accidents. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

Updated on August 09, 2022

Comments

  • UNeverNo
    UNeverNo almost 2 years

    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?