The spreadsheet still displays, but with the warning message. The problem seems to occur because Excel 2007 is more picky about formats matching their extensions than earlier versions of Excel.
The problem was initially discovered by an ASP.Net program and produces in the Excel error "The file you are trying to open, "Spreadsheet.aspx-18.xls', is in a different format than specified by the file extension. Verify ...". However, when I open the file it displays just fine. I am using Excel 2007. Firefox identifies the file as an Excel 97-2003 worksheet.
Here is an ASP.NET page which generates the problem:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spreadsheet.aspx.cs" Inherits="Spreadsheet" %>
The code behind file looks like:
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");
}
}
T
Use
content-type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
And specify extension as xlsx
http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx
That is a link basically describing that MS knows about the problem your describe and that it cannot be suppressed from within ASP.NET code. It must be suppressed/fixed on the client's registry.
I have seen this question asked many times. I ran into the same difficulty today so I fixed the problem using NPOI npoi.codeplex.com/
Hope you find this helpful.
I was trying to resolve this issue during some days. Finally, I've found the solution here: http://www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-file-extension.aspx
Namespaces:
Code:
I aam more fond of using a Grid and changing the response type I have yet to have a problem with that methodology. I have not used straight tab delimited files. One possibility is the \n might have to be \r\n. Just a blind shot.
If you're like me and generating the Excel Sheet as 2003 XML document, you can remove the warnings by doing the following:
Added to the XML output:
Added to the download page:
Now Excel 2007 will not display a warning that the file content and file extension don't match.