Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i'm using the below code toexport the gridview to Excel,i got code from net..so any one pls let me knw the process of the code.....
C++
Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition","attachment;filename=g.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter ht = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(ht);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
Posted
Comments
shanthikalai 8-Mar-11 6:53am    
and also y we use render in above code
Wild-Programmer 8-Mar-11 7:27am    
RenderControl outputs server control content and stores tracing information about the control if tracing is enabled.
sjelen 4-May-12 13:15pm    
This is not real export to excel - you are making a html file with .xls extension and rely on Excel to convert it for you.

Hi,

Use 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 = dataTable; 
gv.DataBind();
gv.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
 
Share this answer
 
Comments
Suraj S Koneri 27-Jun-12 5:09am    
Thanks it helped to solve my problem :)
Omkar Joshi 23-Jul-13 10:09am    
where we get xls sheet which was exported
from grid?
this[^] might help you.
 
Share this answer
 
Check this tutorial:
Export gridview or repeater data to excel with custom results and styles
http://www.magic-dev.com/export-to-excel.htm[^]
 
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