Home > Articles > ASP.NET Articles > How to export data to excel from gridview and DataTable using asp.net?

How to export data to excel from gridview and DataTable using asp.net?

by Thamil   on Jul 28, 2013   Category: ASP.NET  | Level: Intermediate  |  Views: 56467    |  Points: 100   
Like this article? Bookmark and Share:
In this article we will see how to export data from gridview control and dataTable to excel file in a simple way.


This is common requirement for all asp.net application and frequently asked questions from developers as well. There are different ways to export gridview data to excel and here I’m explaining a simple way to export gridview and datatable data to excel. Follow the below steps to achieve this.

Export to Excel from Gridveiw:

Create an asp.net project then drag and drop the gridview control. Design the grid view control as like below.

        <asp:GridView ID="GridVwExportExcel" runat="server" AutoGenerateColumns="False" Font-Names="Verdana"
            PageSize="5" Width="55%">
            <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" />
            <HeaderStyle Height="25px" Font-Size="15px" />
            <RowStyle Height="20px" Font-Size="13px" />
                <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" HeaderStyle-Width="150px" />
                <asp:BoundField DataField="Emp_job" HeaderText="Job title" HeaderStyle-Width="150px" />
                <asp:BoundField DataField="Emp_Dep" HeaderText="Department" HeaderStyle-Width="150px" />
    <br />
        <asp:Button ID="btnExport1" runat="server" Text="Export to Excel from Gridview" OnClick="btnExport1_Click"
            Style="height: 26px" Width="288px" />
    </div><br />
    <asp:Button ID="btnExportfromDt" runat="server" Text="Export to Excel from DataTable"
        Style="height: 26px" onclick="btnExportfromDt_Click" Width="288px"/>

I’m going the bind the employee details with gridview control, so I had created the datatable and added few sample records then binded with gridview control in page load event.

private DataTable LoadData()
            DataTable dt = new DataTable();
            DataRow rw1 = dt.NewRow();
            rw1["Emp_Name"] = "Narain Sidharth";
            rw1["Emp_job"] = "Software Engineer";
            rw1["Emp_Dep"] = "IT";
            DataRow rw2 = dt.NewRow();
            rw2["Emp_Name"] = "Prakalathan";
            rw2["Emp_job"] = "System Engineer";
            rw2["Emp_Dep"] = "IT";
            DataRow rw3 = dt.NewRow();
            rw3["Emp_Name"] = "Mathu kumar";
            rw3["Emp_job"] = "Support Enginner";
            rw3["Emp_Dep"] = "IT";
            DataRow rw4 = dt.NewRow();
            rw4["Emp_Name"] = "Arvind";
            rw4["Emp_job"] = "Sr Software Engineer";
            rw4["Emp_Dep"] = "IT";
            DataRow rw5 = dt.NewRow();
            rw5["Emp_Name"] = "Raja ram";
            rw5["Emp_job"] = "Test Engineer";
            rw5["Emp_Dep"] = "IT";
            return dt;
protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                DataTable dtEmp = LoadData();
                GridVwExportExcel.DataSource = dtEmp;

Place the below code in export button clickevent and this will export the gridview data to excel file. First clear the all content output stream from buffer then add the http header to the output stream and specify the content type and using renderControl method of gridview to get html content and write it to output stream.

protected void btnExport1_Click(object sender, EventArgs e)
            string attachment = "attachment; filename=Emp.xls";
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

Below event used to ensure that htmlform controls is rendered for the specific server control at runtime.

public override void VerifyRenderingInServerForm(Control control)


Export to Excel from DataTable:

Below is the one of way to export data from DataTable to excel file, first get the data from datatable and set the export excel file name as like below. Then clear all content output stream from buffer and set the content type as like below. By using for each loop write the column name into excel file directly from datatable then get each row from datatable and write into each cell of excel file, that’s it.

protected void btnExportfromDt_Click(object sender, EventArgs e)
            string strFilename = "EmpDetails.xls";
            UploadDataTableToExcel(LoadData(), strFilename);
        protected void UploadDataTableToExcel(DataTable dtEmp,string filename)
            string attachment = "attachment; filename="+filename;
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;
            foreach (DataColumn dtcol in dtEmp.Columns)
                Response.Write(tab + dtcol.ColumnName);
                tab = "\t";
            foreach (DataRow dr in dtEmp.Rows)
                tab = "";
                for (int j = 0; j < dtEmp.Columns.Count; j++)
                    Response.Write(tab + Convert.ToString(dr[j]));
                    tab = "\t";

In this article, we have seen in two different ways to export data from gridvew and datatable, it would help when you work with export functionality in gridview control or Datatable. I have attached sample project here, download and see how it works.
Thanks for reading this article and hope enjoyed this; please leave your comments and suggestion about this article. Thank you.

Like this article? Bookmark and Share:

Most viewed Articles

User Comments

  Re :How to export data to excel from gridview and DataTable using asp.net?   
Posted by Garth Winer
on 1/29/2014 1:54:26 AM
Points : 10

You can import data from datatable and gridview into excel file in .NET with Aspose.Cells for .NET. You can find the code for these two data sources on the link given below:

  Re :How to export data to excel from gridview and DataTable using asp.net?   
Posted by Jack Smith
on 6/17/2014 4:20:04 AM
Points : 10

You can use this component to export into excel 2007 and 2010 files


Submit feedback about this article

Please sign in to post feedback

Latest Posts