首页 文章

Excel XML的MIME类型(ASP.NET 3.5)

提问于
浏览
7

我使用CarlosAG-Dll为我创建一个XML-Excel文件(在MemoryStream中) .

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

我的问题是,我在客户端获得myfile.xls(IE)或myfile.xml.xls(FF),因此从excel获得恼人的安全警告 .

我也尝试使用application / vnd.openxmlformats-officedocument.spreadsheetml.sheet(xlsx),但它甚至都不会打开 .

所以我需要剪切.xml并将其作为vnd.ms-excel(如何?)发送或者采用另一种MIME类型(但是哪一种?) .


编辑:我发现了一个错误描述here

我想知道这是否仍然是开放的,为什么?

3 回答

  • 0

    像这样使用

    Response.ContentType = "application/vnd.ms-excel";
    
    Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");
    

    对于Excel 2007及更高版本,MIME类型不同

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
    Response.AppendHeader("content-disposition", "attachment; filename=myfile.xlsx");
    

    请参阅MIME类型列表

    Office 2007 File Format MIME Types

    EDIT:

    如果内容不是本机Excel文件格式,而是基于文本的格式(如CSV,TXT,XML),则网站可以将以下HTTP标头添加到其GET响应中,以告知IE使用备用名称和名称,您可以将扩展名设置为正确的内容类型:Response.AddHeader“Content-Disposition”,“Attachment; Filename = myfile.csv”

    有关详细信息,请参阅 this link

  • 0

    安全警告不是关于MIME类型 - 它是客户端安全设置,您无法从服务器端禁用!

    另一点 - 将_1692391改为:

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

    要么

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

    供参考,请参阅http://www.ietf.org/rfc/rfc2183.txt

    编辑 - 根据评论:

    如果格式不是XLSX(Excel 2007及更高版本),则在上面的代码中使用 myfile.xls .

  • 7

    如果您的文档是Excel Xml 2003文档,则应使用text / xml内容类型 .

    Response.ContentType = "text/xml";
    

    不要指定内容处理 .

    这种技术适用于Handler,而不适用于WebForm .

相关问题