Export Gridview to Excelusing System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using...
Response.Clear();        Response.AddHeader("content-disposition","attachment;filename=GridView1.xls");        Response.Ch...
Upcoming SlideShare
Loading in …5
×

Export gridview to excel

796 views
723 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
796
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Export gridview to excel

  1. 1. Export Gridview to Excelusing System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("Products.xml")); GridView1.DataSource = ds; GridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) {
  2. 2. Response.Clear(); Response.AddHeader("content-disposition","attachment;filename=GridView1.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; StringWriter StringWriter = new System.IO.StringWriter(); HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter); GridView1.RenderControl(HtmlTextWriter); Response.Write(StringWriter.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that a Form control was rendered */ }}Download Source Code

×