Code to export data to excel via asp.net  

Posted by: Quadtechindia in

if (GridView1.Rows.Count > 0)
{

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

Response.Write("Add anything (tag or text) as per requirement");

GridView1.HeaderStyle.Font.Bold = true;
GridView1.HeaderStyle.BackColor = System.Drawing.Color.Black;


System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
this.Controls.Add(frm);

frm.Controls.Add(GridView1);
frm.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

}

This entry was posted on Wednesday, January 20, 2010 and is filed under . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

0 comments

Post a Comment