在Excel 2007中打开时,Excel电子表格生成结果为“与扩展错误不同的文件格式”

电子表格仍然显示,但带有警告消息。 这个问题似乎发生,因为Excel 2007比早期版本的Excel更符合其扩展的格式。

这个问题最初是由一个ASP.Net程序发现的,并且在Excel错误中产生“你试图打开的文件”Spreadsheet.aspx-18.xls“与文件扩展名指定的格式不同。 validation…“然而,当我打开文件时,它显示得很好,我正在使用Excel 2007. Firefox将文件标识为Excel 97-2003工作表。

这是一个生成问题的ASP.NET页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spreadsheet.aspx.cs" Inherits="Spreadsheet" %> 

文件后面的代码如下所示:

 public partial class Spreadsheet : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "application/vnd.ms-excel"; Response.Clear(); Response.Write("Field\tValue\tCount\n"); Response.Write("Coin\tPenny\t443\n"); Response.Write("Coin\tNickel\t99\n"); } 

}

Ť

http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx

这是一个链接,基本上描述了MS知道你所描述的问题,并不能从ASP.NET代码中压制。 它必须被压制/固定在客户的registry上。

如果您像我一样生成Excel表格作为2003 XML文档,则可以通过执行以下操作来删除警告:

添加到XML输出中:

 <?xml version="1.0" encoding="utf-16"?> <?mso-application progid="Excel.Sheet"?> ... 

添加到下载页面:

 // Properly outputs the xml file response.ContentType = "text/xml"; // This header forces the file to download to disk response.AddHeader("content-disposition", "attachment; filename=foobar.xml"); 

现在,Excel 2007不会显示文件内容和文件扩展名不匹配的警告。

我曾多次看到这个问题。 今天我遇到同样的困难,所以我使用NPOI npoi.codeplex.com/解决了这个问题

 public static class ExcelExtensions { /// <summary> /// Creates an Excel document from any IEnumerable returns a memory stream /// </summary> /// <param name="rows">IEnumerable that will be converted into an Excel worksheet</param> /// <param name="sheetName">Name of the Ecel Sheet</param> /// <returns></returns> public static FileStreamResult ToExcel(this IEnumerable<object> rows, string sheetName) { // Create a new workbook and a sheet named by the sheetName variable var workbook = new HSSFWorkbook(); var sheet = workbook.CreateSheet(sheetName); //these indexes will be used to track to coordinates of data in our IEnumerable var rowIndex = 0; var cellIndex = 0; var excelRow = sheet.CreateRow(rowIndex); //Get a collection of names for the header by grabbing the name field of the display attribute var headerRow = from p in rows.First().GetType().GetProperties() select rows.First().GetAttributeFrom<DisplayAttribute>(p.Name).Name; //Add headers to the file foreach (string header in headerRow) { excelRow.CreateCell(cellIndex).SetCellValue(header); cellIndex++; } //reset the cells and go to the next row cellIndex = 0; rowIndex++; //Inset the data row foreach (var contentRow in rows) { excelRow = sheet.CreateRow(rowIndex); var Properties = rows.First().GetType().GetProperties(); //Go through each property and inset it into a single cell foreach (var property in Properties) { var cell = excelRow.CreateCell(cellIndex); var value = property.GetValue(contentRow); if (value != null) { var dataType = value.GetType(); //Set the type of excel cell for different data types if (dataType == typeof(int) || dataType == typeof(double) || dataType == typeof(decimal) || dataType == typeof(float) || dataType == typeof(long)) { cell.SetCellType(CellType.NUMERIC); cell.SetCellValue(Convert.ToDouble(value)); } if (dataType == typeof(bool)) { cell.SetCellType(CellType.BOOLEAN); cell.SetCellValue(Convert.ToDouble(value)); } else { cell.SetCellValue(value.ToString()); } } cellIndex++; } cellIndex = 0; rowIndex++; } //Set the width of the columns foreach (string header in headerRow) { sheet.AutoSizeColumn(cellIndex); cellIndex++; } return workbook.GetDownload(sheetName); } /// <summary> /// Converts the NPOI workbook into a byte array for download /// </summary> /// <param name="file"></param> /// <param name="fileName"></param> /// <returns></returns> public static FileStreamResult GetDownload(this NPOI.HSSF.UserModel.HSSFWorkbook file, string fileName) { MemoryStream ms = new MemoryStream(); file.Write(ms); //.Save() adds the <xml /> header tag! ms.Seek(0, SeekOrigin.Begin); var r = new FileStreamResult(ms, "application/vnd.ms-excel"); r.FileDownloadName = String.Format("{0}.xls", fileName.Replace(" ", "")); return r; } /// <summary> /// Get's an attribute from any given property /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="propertyName"></param> /// <returns></returns> public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property.GetCustomAttributes(attrType, false).First(); } } 

希望你觉得这有帮助。

我在一些日子里试图解决这个问题。 最后,我在这里find了解决scheme: http : //www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-在-A-不同格式的高于指定由这-文件extension.aspx

命名空间:

 using System.IO; using System.Data; using ClosedXML.Excel; 

码:

 DataTable dt = new DataTable("GridView_Data"); // Fill your DataTable here... //Export: using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx"); using (MemoryStream MyMemoryStream = new MemoryStream()) { wb.SaveAs(MyMemoryStream); MyMemoryStream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); } } 

我更喜欢使用Grid并改变响应types,但是我还没有用这个方法来解决问题。 我没有使用直接制表符分隔的文件。 一种可能性是\ n可能必须是\ r \ n。 只是一个盲目的镜头。

使用

内容types=应用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet

并指定扩展名为xlsx