Click here to Skip to main content
15,564,534 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I am trying to export a gridview data to excel. I tried using the code I got from the internet, as per comments it's working for them. But I got excel 2010/2007

string fileName = "attachment;filename= DetailReport.xlsx";
            Response.Clear();
            Response.AddHeader("content-disposition", fileName);
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            grdExcel.RenderControl(htmlWrite);
            Response.Write(stringWrite);
            Response.End();


The file downloads, but when I open it I got an error that the file is in a different format. Any ideas? I searched the internet and there is no clear solution that I can get.

Thanks
Posted
Updated 29-Jul-20 3:41am
Comments
Franco Cipriano 19-Mar-13 9:30am    
Hi,

I tried this code:

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdExcel.AllowPaging = false;
grdExcel.DataBind();
grdExcel.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

and also I followed the instruction about extension hardening. see this article: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

..now the files can open and it does not give me an error..the problem now is that it did not populate the rows and column that are in the gridview, it's basically just a blank worksheet
Member 9581488 19-Mar-13 10:08am    
where you are binding your gridview to respected datasource?

 
Share this answer
 
Comments
Franco Cipriano 19-Mar-13 8:13am    
Hi,

I followed the solution and it still gives me a corrupted file
Avik Ghosh22 19-Mar-13 8:56am    
reintall microsoft office...
string fileName = "attachment;filename= DetailReport.xlsx";
     Response.Clear();
     Response.AddHeader("content-disposition", fileName);
     Response.Charset = "";
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
     Response.ContentType = "application/vnd.ms-excel";
     System.IO.StringWriter stringWrite = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     grdExcel.RenderControl(htmlWrite);
     Response.Output.Write(stringWrite.ToString());
     Response.Flush();
     Response.End();



Try it with above code.
Hope that helps.
 
Share this answer
 
Comments
Franco Cipriano 19-Mar-13 8:13am    
Hi,

I still get a corrupted file
Aroon Kumar 11-Nov-19 6:39am    
I need to change the extension from xlsx to xls then only its wirking
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachmant;filename={0}", "excel.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();





It works!!!
 
Share this answer
 
v2
Comments
CHill60 3-Jul-14 6:00am    
Bit late though.
Hi check the below solution for correct solution

http://www.dotnetpools.com/2012/09/gridview-export-to-excel-in-aspnet-c.html[^]
 
Share this answer
 

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