«

»

Jul 19

how to export gridview data to excel in asp.net using c#

In this article. I am going to explain about how to Export Data from grid view to Excel sheet:

1.In many cases its very difficult to check the entire data in grid view,so that we can export data into

excel and then we can check  easily.

2.To do that you have to save dataset data in view state and then bind to excel as below:

3.Open sql server create new table “testexport” as below figure.

 

1

4.Insert data into table through your easiest way and bin data to grid view as below figure

 

 

 

2

 

 

5.Click on export button you will get a popup as below

 

 

3

6.Output:-

4

 

5.Click on export button you will get a popup as below

6.Output

Code for this:

In Default.aspx place this Body:

[code language=”html”]

<body>

<form id="form1" runat="server">

<div>

<fieldset id="GdvData" style="width: 25%; text-align: center">

<legend>Grid Data:</legend>

<table>

<tr>

<asp:GridView ID="GDVData" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:BoundField DataField="FirstName" HeaderText="First Name" />

<asp:BoundField DataField="Lastname" HeaderText="Last Name" />

<asp:BoundField DataField="Address" HeaderText="Address" />

</Columns>

</asp:GridView>

</tr>

</table>

</fieldset>

</div>

<div>

<table>

<tr>

<td align="center">

<asp:Button ID="btnExcell" runat="server" Text="Export to Excell" OnClick="btnExcell_OnClick" />

</td>

</tr>

</table>

</div>

</form>

</body>

[/code]

On Default.cs Place this code:

[code language=”csharp”]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.IO;

using System.Text;

public partial class Export : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindData();

}

}

protected void BindData()

{

SqlConnection con = new SqlConnection("server=Naseer-PC;uid=sa;password=123;database=Naseer");

string Query = "select * from TestExport";

SqlDataAdapter da = new SqlDataAdapter(Query, con);

DataSet ds = new DataSet();

da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)

{

GDVData.DataSource = ds;

GDVData.DataBind();

ViewState["ds"] = ds;

}

else

{

ViewState["ds"] = null;

}

}

protected void btnExcell_OnClick(object sender, EventArgs e)

{

try

{

System.IO.StringWriter objStringWriter = new System.IO.StringWriter();

System.Web.UI.WebControls.DataGrid tempDataGrid = new System.Web.UI.WebControls.DataGrid();

System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);

HttpContext.Current.Response.ClearContent();

HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

HttpContext.Current.Response.Charset = "";

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Export.xls");

DataSet ds = (DataSet)ViewState["ds"];

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)

{

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)

{

tempDataGrid.DataSource = ds;

}

}

else

{

tempDataGrid.DataSource = null;

}

tempDataGrid.DataBind();

tempDataGrid.HeaderStyle.Font.Bold = true;

tempDataGrid.RenderControl(objHtmlTextWriter);

HttpContext.Current.Response.Write(objStringWriter.ToString());

HttpContext.Current.Response.End();

}

catch (Exception ex)

{

throw ex;

}

}

}

[/code]

Happy Coding!!!

Admin.

4 comments

Skip to comment form

  1. Qutub

    How to export GridView data to excel in asp.net in C#.
    http://goo.gl/CRkMlW

  2. masud parvez

    Dear Concern,
    Thank you a lot for sharing.
    It’s helped me a lot.

  3. ayesha

    very informative. thanks

    I have found another example for the same to excel refer to http://www.etechpulse.com/2014/04/exporting-div-dynamic-content-to-excel.html

  4. parshvanath

    how to import data from excel sheet

Leave a Reply

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

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