Export GridView Data to Excel Sheet in ASP.Net C#

In this asp.net article i will explain you how to export gridview control data to ms Excel Sheet in asp.net with C#.

First Design asp.net form with Gridview contorl and one Button control.

Here, we need to bind data from server to Gridview control on page load event.

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

The HTML Design code is :

 <asp:GridView ID=”GridView1″ runat=”server” BackColor=”White”
BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
</asp:GridView>
<br />
<asp:Button ID=”Button1″ runat=”server” Font-Bold=”True” Height=”34px”
onclick=”Button1_Click” Text=”EXPORT TO EXCEL” />

After Design the asp.net webform as above bind the data to Gridview control in Page_load Event.

First Add name space for sqlconnection and StringWriter as below :

using System.Data.SqlClient;
using System.IO;

Now write below code on page_load event for BindGridview control.

  protected void Page_Load(object sender, EventArgs e)
{
SqlConnection SQLConn = new SqlConnection(“Data Source=’.\\SQLEXPRESS’;Integrated Security=’true’;Initial Catalog=’Example’”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“Select * from UserMst”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

Now , write below code on Export to Excel Button control in asp.net code page:

protected void Button1_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader(“content-disposition”,
“attachment;filename=Users.xls”);
Response.ContentType = “applicatio/excel”;
StringWriter sw = new StringWriter(); ;
HtmlTextWriter htm = new HtmlTextWriter(sw);
GridView1.RenderControl(htm);
Response.Write(sw.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{

}

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

 

I hope this Export GridView Data to Excel Sheet in ASP.Net will help you….

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 1 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">