在Asp.net MVC网站中生成Excel XML文档 [英] Generate an Excel XML document in Asp.net MVC Web Site
问题描述
我尝试将ContentType更改为Excel XLS格式(application / excel),并使Excel打开文件但是它会给出一条警告消息:该文件是一个XML文档,而不是XLS文档。
如何使Excel中的Excel文档从网站打开?
<%@ 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: w3.org/TR/REC-html40\">
您的问题可能是使用文件扩展名.xml ,您在标题中添加。
我做同样的事情的代码根本没有文件扩展名。
您可以尝试删除.xml扩展名,或将其更改为.xls或.csv。
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">
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.
这篇关于在Asp.net MVC网站中生成Excel XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!