Create an Excel XML document on Asp.net MVC

I have an ASP.Net MVC site that creates a Microsoft Excel 2003 spreadsheet with formatting. The spreadsheet looks good, the controller and views work, but the file does not open in Excel. It opens in a browser because it is an XML document.

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

How to make an Excel XML document open in Excel from a website?

<%@ 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"> 
+3
asp.net-mvc excel export-to-excel
Dec 22 '09 at 20:06
source share
2 answers

Your problem may be using the .xml file extension that you add to your header. My code that does the same has no file extension at all.

You can try to remove this .xml extension or change it to .xls or .csv.

+1
Dec 22 '09 at 20:19
source share

Try

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

And save the .XML extension.

+3
Jan 23 '10 at 2:05
source share



All Articles