Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
Hi all,

I am trying to export data to an excel file [.xlsx file].
but I am unable to open that file.
Its giving warning message
"Excel cannot open the file 'MyExport.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"

Is any one knows the solution how to resolve this 2007 excel export issue.
C#
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
myGrid.RenderControl(hw);

Response.Clear();
Response.Buffer = true;       
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Write(sw);
Response.End();
Posted
Updated 8-Nov-11 19:43pm
v2

try this code i think it will help you....
but one thing i clear that if your have put your gridview inside UpdatePanel Then any of your code will not work and its give the error like this...

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.



put your grid out side the UpdatePanel and use this code it will work for me...
C#
protected void Export2Excel()
{
    try
    {
        this.GridView1.AllowPaging = false;
        this.GridView1.AllowSorting = false;
        this.GridView1.EditIndex = -1;

        // Let's bind data to GridView
        this.BindData();

        // Let's output HTML of GridView
        Response.Clear();
        Response.ContentType = "application/vnd.xls";
        // OR .xlsx file use this..
        // ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

        Response.AddHeader("content-disposition",
                "attachment;filename=MyList.xls");
        Response.Charset = "";
        StringWriter swriter = new StringWriter();
        HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
        GridView1.RenderControl(hwriter);
        Response.Write(swriter.ToString());
        Response.End();
    }
    catch (Exception exe)
    {
        throw exe;
    }
 
}       
 
Share this answer
 
Comments
K V Sekhar 9-Nov-11 4:12am    
Hi Vaishnav,
Thanks for your quick reply.
But its not useful to me, My requirement is export data to 2007 office excel file not lower versions.
I could able to export data to .xls files, but i wants to export it to .xlsx file. This is where i am getting warnings.

Plz let me know if you have any solution for this.
Tejas Vaishnav 9-Nov-11 5:02am    
Excel 2007 cannot recognize the pure html format. You'd better use xls as the extension or render the excel format to client end.
Refer to the similar question How to export gridview to excel ?[^], that was answered earlier.

Also, make sure you have mime map configured for xlsx in IIS.
 
Share this answer
 
Comments
K V Sekhar 9-Nov-11 4:59am    
I need to export data to excel 2007 version not later versions.
Also can't it possible to export data without configuring XLSX in IIS.
Because client machine there is no IIS.
NaVeN Kumar 1-Apr-13 1:11am    
Hello Sekahr,

Have u got any solution for your question because I am also having same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900