Faizan
Faizan

Reputation: 552

xlsx File format is not valid

I have used System.IO to export Report in Excel. It is working fine for .xls extension but when I changed extension to .xlsx, it gives an error i.e. Incorrect extension or Incorrect File Format.

var grdview = new GridView();
            grdview.DataSource = this.GetSequence();
            grdview.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename= Search Report " + Date + " - " + Date1 + ".xls ");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter strWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            htmlWriter.Write("<table><tr><td colspan='13' align='center'><font size='45'>Search Report of Regular Passenger</font></td></tr></table>");
            grdview.RenderControl(htmlWriter);
            Response.Output.Write(strWriter.ToString());
            Response.Flush();
            Response.End();

What is the problem?

Upvotes: 0

Views: 510

Answers (1)

Qwertiy
Qwertiy

Reputation: 21430

The problem is that html is niether xls nor xlsx.

For xls as an obsolete format Excel tries to fix your problem and check for all formats it supports:

screensot

But xlsx should be zip with some xmls inside, so the message differs:

screenshot

You have to use corresponding library to create real excel files.

Upvotes: 1

Related Questions