在 Asp.net MVC 网站中生成 Excel XML 文档
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
并保留 .XML 扩展名。
Try with
And keep the .XML extension.
您的问题可能在于使用文件扩展名 .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.