Click here to Skip to main content
15,881,631 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Article

Export GridView records to Excel

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 7.1K   2  
In this Article we are going to read and understand how in a web application we can export a gridview data into Excel file. As many times in real

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

In this Article we are going to read and understand how in a web application we can export a gridview data into 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.

Prerequisites:

  1. To view an Excel workbook file's contents, you must have installed Microsoft Excel (alone or with MS-Office) to view the exported file in your System.
  2. Microsoft Visual Studio (VS) must be installed on the system for development. 

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. Create a datatable which will bind the grid.
  4. Place a button which will export the grid to excel format.

Code:

<span style="font-family:';color:blue;">protected</span><span style="font-family:';"> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span>EventArgs</span> e)</span>
<span style="font-family:';"><span style="font-size:0px;"></span>{</span>
<span style="font-family:';"><span style="color:green;"> //creating a table for the grid use namespace System.Data;</span></span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> DataTable </span><span style="font-size:0px;"> </span>dt = <span style="color:blue;">new</span> <span>DataTable</span> ();</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:green;"> //adding columns to the datatale</span></span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> try</span></span>
<span style="font-family:';"><span style="color:#0000ff;" color="#0000ff"> </span>{</span>
<span style="font-family:';"><span style="font-size:0px;"></span> dt.Columns.Add(<span>"Srno"</span>);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> dt.Columns.Add(<span>"Name"</span>);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> catch</span> { }</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:green;"> //adding values to the datatable</span></span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> for</span> (<span style="color:blue;">int</span> i = 1; i <= 10; i++) <span style="font-size:0px;"></span></span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> DataRow</span> dr = dt.NewRow();</span>
<span style="font-family:';"><span style="font-size:0px;"></span> dr[0] = i;</span>
<span style="font-family:';"><span style="font-size:0px;"></span> dr[1] = <span>"Meetu Choudhary "</span> + i.ToString();</span>
<span style="font-family:';"><span style="font-size:0px;"></span> dt.Rows.Add(dr);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:green;"> //binding data table to the grid </span></span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="font-size:0px;"></span> grdtoexport.DataSource = dt;</span>
<span style="font-family:';"><span style="font-size:0px;"></span> grdtoexport.DataBind();</span>
<span style="font-family:';"><span style="font-size:0px;"></span>}</span>

  Writing a ExportToExcel class

  using System;

<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Collections.Generic;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Linq;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Web;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Data;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Configuration;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Web.UI;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Web.UI.WebControls;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Web.UI.WebControls.WebParts;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Web.UI.HtmlControls;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.Text;</span>
<span style="font-family:';color:blue;">using</span><span style="font-family:';"> System.IO;</span>


<span style="font-family:';color:blue;">namespace</span><span style="font-family:';"> ExportToExcel</span>
<span style="font-family:';">{</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:gray;"> </span></span><span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span>ExportToExcel</span></span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> public</span> ExportToExcel()</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> public</span> <span style="color:blue;">void</span> ExportGridView(<span>GridView</span> GridView1, <span>String</span> strFileName)</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span> PrepareGridViewForExport(GridView1);</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:green;"> </span></span><span style="font-family:';"><span>HttpContext</span>.Current.Response.ClearContent();</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.Buffer = <span style="color:blue;">true</span>;</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.AddHeader(<span>"content-disposition"</span>, <span>"attachment;filename="</span> + strFileName);</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.ContentType = <span>"application/ms-excel"</span>;</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.Charset = <span>""</span>;</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="font-size:0px;"></span><span style="color:green;"> </span></span><span style="font-family:';"><span style="font-size:0px;"></span><span>StringWriter</span> sw = <span style="color:blue;">new</span> <span>StringWriter</span>();</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HtmlTextWriter</span> htw = <span style="color:blue;">new</span> <span>HtmlTextWriter</span>(sw);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> GridView1.RenderControl(htw);</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.Write(sw.ToString());</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> HttpContext</span>.Current.Response.End();</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>

<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> private</span> <span style="color:blue;">void</span> PrepareGridViewForExport(<span>Control</span> gv)</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> LinkButton</span> lb = <span style="color:blue;">new</span> <span>LinkButton</span>();</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span> Literal</span> l = <span style="color:blue;">new</span> <span>Literal</span>();</span>
<span style="font-family:';"><span style="color:blue;"> string</span> name = <span>String</span>.Empty;</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> for</span> (<span style="color:blue;">int</span> i = 0; i < gv.Controls.Count; i++)</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> if</span> (gv.Controls[i].GetType() == <span style="color:blue;">typeof</span>(<span>LinkButton</span>)) //Link Button Text property</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span> l.Text = (gv.Controls[i] <span style="color:blue;">as</span> <span>LinkButton</span>).Text;</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.Remove(gv.Controls[i]);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.AddAt(i, l);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="color:blue;"> else</span> <span style="color:blue;">if</span> (gv.Controls[i].GetType() == <span style="color:blue;">typeof</span>(<span>DropDownList</span>))</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span> l.Text = (gv.Controls[i] <span style="color:blue;">as</span> <span>DropDownList</span>).SelectedItem.Text; //Drop down Selected Text</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.Remove(gv.Controls[i]);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.AddAt(i, l);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="color:blue;"> else</span> <span style="color:blue;">if</span> (gv.Controls[i].GetType() == <span style="color:blue;">typeof</span>(<span>CheckBox</span>)) //Convert Check Box to True and False</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span> l.Text = (gv.Controls[i] <span style="color:blue;">as</span> <span>CheckBox</span>).Checked ? <span>"True"</span> : <span>"False"</span>;</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.Remove(gv.Controls[i]);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> gv.Controls.AddAt(i, l);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="color:blue;"> if</span> (gv.Controls[i].HasControls())</span>
<span style="font-family:';"><span style="font-size:0px;"></span> {</span>
<span style="font-family:';"><span style="font-size:0px;"></span><span style="font-size:0px;"></span> PrepareGridViewForExport(gv.Controls[i]);</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';"><span style="font-size:0px;"></span> }</span>
<span style="font-family:';">}</span>

 

  Calling the Function to export on button click

<span style="font-family:';color:blue;">protected</span><span style="font-family:';"> <span style="color:blue;">void</span> btnexport_Click(<span style="color:blue;">object</span> sender, <span>EventArgs</span> e)</span>
<span style="font-family:';"></span><span style="font-family:';">{</span>
<span style="font-family:';"><span> ExportToExcel</span> ex = <span style="color:blue;">new</span> <span>ExportToExcel</span>();</span>
<span style="font-family:';"><span style="font-size:0px;"></span> ex.ExportGridView(grdtoexport, <span>"Client.xls"</span>);<span style="font-size:0px;"> </span></span>
<span style="font-family:';"><span style="font-size:0px;"></span>}</span>

 

You can download the code from here


Regards, Meetu Choudhary
Microsoft MVP-ASP/ASP.NET

MsDnM My Forums My Blog

Orignal Article

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --