Exporting Data from grid view to Excel file in asp.net

How to export Data from grid view to Excel file in asp.net

Solution

Type Following Code for export Data from grid view to Excel file in asp.net

Default.aspx

<asp:Button ID=”Button1″ Text=”Export to Excelsheet” runat=”server”
Width=”150px” OnClick=”Button1_Click” />

 

Default.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader(“content-disposition”, “attachment;filename=Export.xls”);
Response.Charset = “”;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = “application/vnd.xls”;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgexcel.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

 

Note : dgexcel is a grid view id

 

 

 

 

About the Author

avatar