English 中文(简体)
Generate an Excel XML document in Asp.net MVC Web Site
原标题:

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.

问题回答

Try with

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

And keep the .XML extension.





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签