.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export Grid To Excel

Posted By:Meetu Choudhary       Posted Date: August 14, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Export DataGrid to Excel in Asp.Net


In this article we are going to read and understand how in a web application we can export a grid data in the excel file. As many times in real time programming we generate reports in the grid format to display to the user.

For example

1 .The list of commodities purchased this month

2. Reports of SMS Sent.

3. Reports of invites. etc.and user might want to save this list for the future use. In Excel format then we need to export this grid to the excel.


1. To view an Excel workbook file's contents, you must have installed Microsoft Excel (alone or with MS-Office) on your system.

2. Microsoft Visual Studio (VS) must be installed on the (I haven't tested it on the Prior versions)

Let's start with creating an application in VS2008 (You can even go for VS2005 or VS2010)

Following the steps to we are going to follow.

  1. Create a new project in VS2008 as name it as "ExporttoExcel" in the C# category.
  2. Place a gridview on the default.aspx page and rename it to grdtoexport.
  3. And place a button which will export the grid to excel.
  4. Now lets create a datatable which will bind the grid.

The Code will look like:

protected void Page_Load(object sender, EventArgs e)


//creating a table for the grid use namespace System.Data;

DataTable dt = new DataTable ();

//adding columns to the datatale






catch { }

//adding values to the datatable

for (int i = 1; i <= 10; i++)


DataRow dr = dt.NewRow();

dr[0] = i;

dr[1] = "Meetu Choudhary " + i.ToString();



//binding databale to the grid

grdtoexport.DataSource = dt;



Writing a ExportToExcel class

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data;

using System.Configuration;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.IO;

namespace ExportToExcel



/// Summary description for ExportToExcel


public class ExportToExcel


public ExportToExcel()



// TODO: Add constructor logic here



public void ExportGridView(GridView GridView1, String strFileName)



//string attachment = "attachment; filename=Contacts.xls";


HttpContext.Current.Response.Buffer = true;

HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);

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

HttpContext.Current.Response.Charset = "";

//System.Web.UI.Page.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);





private void PrepareGridViewForExport(Control gv)


LinkButton lb = new LinkButton();

Literal l = new Literal();

string name = String.Empty;

for (int i = 0; i < gv.Controls.Count; i++)


if (gv.Controls[i].GetType() == typeof(LinkButton))


l.Text = (gv.Controls[i] as LinkButton).Text;


gv.Controls.AddAt(i, l);


else if (gv.Controls[i].GetType() == typeof(DropDownList))


l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;


gv.Controls.AddAt(i, l);


else if (gv.Controls[i].GetType() == typeof(CheckBox))


l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";


gv.Controls.AddAt(i, l);


if (gv.Controls[i].HasControls())






/*Use this commented function in all the pages where the above export function is used

//public override void VerifyRenderingInServerForm(Control control)





Calling the Function to export on button click

protected void btnexport_Click(object sender, EventArgs e)


ExportToExcel ex = new ExportToExcel();

ex.ExportGridView(grdtoexport, "Client.xls");


You can download the code from here


For More Details Visit :Here or Here

Regards, Meetu Choudhary


 MsDnM || My Forums || My Blog

 Subscribe to Articles


Further Readings:

Author: BarbaMariolino         Company URL: http://www.dotnetspark.com
Posted Date: September 01, 2009

Great article! But lot of people don''t have Microsoft Excel installed. You need to try GemBox.Spreadsheet. It doesn''t need Excel to be installed to work. This comes very hands when you are building commercial applications.

Author: Meetu Choudhary         Company URL: http://www.dotnetspark.com
Posted Date: September 01, 2009

Even in this you also need not to have excel installed on the client system to execute. but yes if you want to see the output excel file you do need that.
Author: Peter         Company URL: http://www.dotnetspark.com
Posted Date: July 07, 2010

nice articles, There is a excel component without excel automation, very useful to me, to export grid to excel just one line of code, recommand to you.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend