在 Asp.net MVC 网站中生成 Excel XML 文档

发布于 2024-08-16 05:21:52 字数 1076 浏览 2 评论 0原文

我有一个 ASP.Net MVC 站点,可以生成 Microsoft Excel 2003 XML 格式的电子表格。电子表格看起来不错,控制器和视图都可以工作,但文件无法在 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">  

I have an ASP.Net MVC site that generates a Microsoft Excel 2003 XML formatted spreadsheet. The spreadsheet looks good, the controller and views both work, but the file won't open in Excel. It opens in the browser because it is an XML document.

I tried changing the ContentType to be the Excel XLS format (application/excel) and that made Excel open the file but it gives a warning message that the file is an XML document, not an XLS document.

How do you make an Excel XML document open in Excel from a web site?

<%@ 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">  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-08-23 05:21:52

尝试

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

并保留 .XML 扩展名。

Try with

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

And keep the .XML extension.

做个ˇ局外人 2024-08-23 05:21:52

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

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

Your problem may be in using the file extension .xml, which you're adding in your header.
My code that does this same thing does not have a file extension at all.

You might try dropping that .xml extension, or changing it to .xls or .csv.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文