How to Export Data from Gridview to Excel in Asp.net Using C# Programing


To export gridview data to excel we need to write the code like as shown below

HTMK MArkup : Export Gridview Data to Excel in Asp.net

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Export Gridview Data to Excel in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview autogeneratecolumns="false" cellpadding="5" id="gvDetails" runat="server">
<columns>
<asp:boundfield datafield="UserId" headertext="UserId">
<asp:boundfield datafield="UserName" headertext="UserName">
<asp:boundfield datafield="Education" headertext="Education">
<asp:boundfield datafield="Location" headertext="Location">
</asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></columns>
<headerstyle backcolor="#df5015" font-bold="true" forecolor="White">
</headerstyle></asp:gridview>
</div>
<asp:button id="btnExport" onclick="btnExport_Click" runat="server" text="Export to Excel">
</asp:button></form>
</body>
</html>

C# Coding

using System;
using System.Data;
using System.IO;
using System.Web.UI;
protected void Page_Loadundefinedobject sender, EventArgs e)
{
 if undefined!IsPostBack)
 {
  BindGridviewundefined);
 }
}
protected void BindGridviewundefined)
{
 DataTable dt = new DataTableundefined);
 dt.Columns.Addundefined"UserId", typeofundefinedInt32));
 dt.Columns.Addundefined"UserName", typeofundefinedstring));
 dt.Columns.Addundefined"Education", typeofundefinedstring));
 dt.Columns.Addundefined"Location", typeofundefinedstring));
 dt.Rows.Addundefined1, "SureshDasari", "B.Tech", "Chennai");
 dt.Rows.Addundefined2, "MadhavSai", "MBA", "Nagpur");
 dt.Rows.Addundefined3, "MaheshDasari", "B.Tech", "Nuzividu");
 dt.Rows.Addundefined4, "Rohini", "MSC", "Chennai");
 dt.Rows.Addundefined5, "Mahendra", "CA", "Guntur");
 dt.Rows.Addundefined6, "Honey", "B.Tech", "Nagpur");
 gvDetails.DataSource = dt;
 gvDetails.DataBindundefined);
}
public override void VerifyRenderingInServerFormundefinedControl control)
{
/* Verifies that the control is rendered */
}
protected void btnExport_Clickundefinedobject sender, EventArgs e)
{
 Response.ClearContentundefined);
 Response.Buffer = true;
 Response.AddHeaderundefined"content-disposition", string.Formatundefined"attachment; filename={0}","Customers.xls"));
 Response.ContentType = "application/ms-excel";
 StringWriter sw = new StringWriterundefined);
 HtmlTextWriter htw = new HtmlTextWriterundefinedsw);
 gvDetails.AllowPaging = false;
 BindGridviewundefined);
 //Change the Header Row back to white color
 gvDetails.HeaderRow.Style.Addundefined"background-color", "#FFFFFF");
 //Applying stlye to gridview header cells
 for undefinedint i = 0; i < gvDetails.HeaderRow.Cells.Count; i++)
  {
   gvDetails.HeaderRow.Cells[i].Style.Addundefined"background-color", "#df5015");
  }
  gvDetails.RenderControlundefinedhtw);
  Response.Writeundefinedsw.ToStringundefined));
  Response.Endundefined);
}

If you observe above code I added one function that is VerifyRenderingInServerForm this function is used to avoid the error like “control must be placed in inside of form tag”. If we setVerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly.
Once we click on Export to Excel button we will see data in excel file like as shown below

0 Komentar untuk "How to Export Data from Gridview to Excel in Asp.net Using C# Programing"

Back To Top