tags:

views:

6648

answers:

6

I am generating a spreadsheet in Asp.Net, using the following code, but am getting the 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. Thanks in advance for any help you can provide.

The aspx page contains:

<%@ 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

A: 

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.

Gregory A Beamer
A: 

Use

content-type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

And specify extension as xlsx

Danny G
+17  A: 

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.

Eric H
I have encountered this, this is the issue.
brendan
The article also provides another fix, that doesn't involve modifying the registry. I added the line: Response.AddHeader("Content-Disposition", "Attachment;Filename=Spreadsheet.csv"); and then generated a comma separated file. I could have used tabs with a .txt file.
Jeff Bloom
Further to Jeff Bloom, see my answer below if you're output is excel as xml
Gavin Miller
A: 

This problem is most probably occuring in Excel 2007. That is why Danny has suggested above solution but this will not work if the client has a previous version of Excel. I liked the comma seperated file approach though and will try it ASAP.

+5  A: 

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:

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

Added to the download page:

// 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");

Now Excel 2007 will not display a warning that the file content and file extension don't match.

Gavin Miller
This means that file doesn't automatically open with excel though...
BombDefused
@BombDefused It should open automatically with excel. If you've got a version of excel > 2003 the line `<?mso-application progid="Excel.Sheet"?>` will facilitate that. If it's not working, it may be because of file associations or an excel setting.
Gavin Miller
+1  A: 

Please refer to :http://support.microsoft.com/kb/948615

Avinash