Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello;

I am using ASP.Net 4.0;

How can we export or convert the data which displayed in Gridview in to Microsoft office EXCEL sheet.

Thanks in Advance..
Posted
Comments
Wild-Programmer 14-Mar-11 9:26am    
Daily number of members get scolding for directly asking questions to common issues before trying themselves. Probably you are new to the forum and ASP.NET. Please try googling. The very first search result is your answer :)

Hi,

Try following code

Response.Clear();
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=ExportData.xls");
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView gv = new GridView();
gv.DataSource = dt; // assign your datatable here that you assign to grid.
gv.DataBind();
gv.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
 
Share this answer
 
Comments
Tony Tom.k 10-Mar-12 4:50am    
using this code we will be able to generate only one excel work book at a time how we can generate different worksheets on same workbook using this code?
 
Share this answer
 
check this link
Export GridView to Excel[^]
 
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