3

我有一个生成 Microsoft Excel 2003 XML 格式电子表格的 ASP.Net MVC 站点。电子表格看起来不错,控制器和视图都可以工作,但文件无法在 Excel 中打开。它在浏览器中打开,因为它是一个 XML 文档。

我尝试将 ContentType 更改为 Excel XLS 格式(应用程序/excel),这使 Excel 打开了文件,但它给出了一条警告消息,指出该文件是 XML 文档,而不是 XLS 文档。

如何从网站在 Excel 中打开 Excel XML 文档?

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><%  
this.Context.Response.Clear();  
this.Context.Response.AddHeader("content-disposition", "attachment;filename=State_Report_" + this.ViewData["State"] + ".xml");  
this.Context.Response.Charset = "";  
this.Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
this.Context.Response.ContentType = "application/excel";  
%><?xml version="1.0"?>  
<?mso-application progid="Excel.Sheet"?>  
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"  
  xmlns:o="urn:schemas-microsoft-com:office:office"  
  xmlns:x="urn:schemas-microsoft-com:office:excel"  
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"  
  xmlns:html="http://www.w3.org/TR/REC-html40">  
4

2 回答 2

3

尝试

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

并保留 .XML 扩展名。

于 2010-01-23T02:05:35.897 回答
1

您的问题可能在于使用您在标题中添加的文件扩展名 .xml。我做同样事情的代码根本没有文件扩展名。

您可以尝试删除该 .xml 扩展名,或将其更改为 .xls 或 .csv。

于 2009-12-22T20:19:11.350 回答