2

I am using the below code for export the data to excel sheet.

    private void ExportToExcel(string fileName)
    {
        fileName = "MyXML.xls";

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        rptLinks.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

Everything is working fine but when I try to open the excel file MyXML.xls by double clicking, a popup/dialog box open with the below message :

"The file you are trying to open , MyXML.xls is in different format than specified by the file extension. " etc..

Can we make some changes in code so that no popup/dialog should come?

Gaurav123
  • 5,059
  • 6
  • 51
  • 81
  • Possible dup of this question http://stackoverflow.com/questions/652377/excel-spreadsheet-generation-results-in-different-file-format-than-extension – Kevin Main Apr 26 '12 at 09:45

2 Answers2

3

Hope this helps

http://devblog.grinn.net/2008/06/file-you-are-trying-to-open-is-in.html

Sugandika
  • 772
  • 5
  • 10
2

change Response.ContentType = "application/vnd.xls"; to Response.ContentType = "application/vnd.ms-excel"

rt2800
  • 3,045
  • 2
  • 19
  • 26
  • http://stackoverflow.com/questions/940045/how-to-suppress-the-file-corrupt-warning-at-excel-download#answer-940082 – edisonmecaj Sep 13 '16 at 11:09