C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Videos


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...


Birthday Greetings
Learn Windows 7: Top 5 free web hosting sites.   This resource will give an idea on free web hosting sites.



Resources » Code Snippets » C# Syntax »

Export GridView to Excel File using Asp.net 2.0


Posted Date: 19 Aug 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Nitin Kumar SharmaMember Level: Silver    
Rating: 1 out of 5Points: 10



I faced a problem yesterday where I have to export a gridview content to excel. The problem was as follows :

User is generating some reports –those reports are displaying in gridview and he also wants to download these reports into a .xls file. So here is the solution-

I created a class GridViewExportToExcel,there is a method: Export which will perform the necessary task. GridViewExportToExcel’s code is given below :-


using System;

using System.Data;

using System.Configuration;

using System.IO;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public class GridViewExportToExcel

{

public static void Export(string fileName, GridView gv)

{

HttpContext.Current.Response.Clear();

//Export will take two parameter first one the name of Excel File, and second one for gridview to be exported
HttpContext.Current.Response.AddHeader(

"content-disposition", string.Format("attachment; filename={0}", fileName));

HttpContext.Current.Response.ContentType = "application/octet-stream";

using (StringWriter strWriter = new StringWriter())

{

using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))

{

// Create a form to contain the grid

Table table = new Table();

// add the header row to the table

if (gv.HeaderRow != null)

{

GridViewExportToExcel.ExportControl(gv.HeaderRow);

table.Rows.Add(gv.HeaderRow);

}

// add each of the data rows to the table

foreach (GridViewRow row in gv.Rows)

{

GridViewExportToExcel.ExportControl(row);

table.Rows.Add(row);

}

// add the footer row to the table

if (gv.FooterRow != null)

{

GridViewExportToExcel.ExportControl(gv.FooterRow);

table.Rows.Add(gv.FooterRow);

}

// render the table into the htmlwriter

table.RenderControl(htmlWriter);

// render the htmlwriter into the response

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

HttpContext.Current.Response.End();

}

}

}

/// Replace controls with literals

private static void ExportControl(Control control)

{

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

{

Control current = control.Controls[i];

if (current is LinkButton)

{

control.Controls.Remove(current);

control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));

}

else if (current is ImageButton)

{

control.Controls.Remove(current);

control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));

}

else if (current is HyperLink)

{

control.Controls.Remove(current);

control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));

}

else if (current is DropDownList)

{

control.Controls.Remove(current);

control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));

}

else if (current is CheckBox)

{

control.Controls.Remove(current);

control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));

}

//Like that you may convert any control to literals

if (current.HasControls())

{

GridViewExportToExcel.ExportControl(current);

}

}

}

}


Using the above code you may export any data representation control’s data to Excel.
Leave comments if its help you and your suggestion are also welcome.
Happy Programming….:)

For more details, visit http://kaniks.blog.co.in/2008/07/31/aspnet-20-export-gridview-to-excel-c/





Responses to the resource: "Export GridView to Excel File using Asp.net 2.0"
Author: FROSTY    19 Feb 2009Member Level: Bronze   Points : 2
I am looking to export a GridView control to Excel. However, here's my catch. The Gridview has 100,000+ rows = multiple pages worth of data.

I have yet to see any C# code that handles more than one page worth of Gridview Data.

Is there a way to do this using the Gridview or should I build the spreadsheet from a direct call to SQL Server???


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
ASP.Net 2.0: Export GridView to Excel  .  The Gridview to Excel Export code in asp.net  .  Exporting GridView to Excel with c#  .  Export GridView to Excel  .  Render cells  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Graph in Crystal Report
Previous Resource: Showing only selected country that is stored in database
Return to Resources
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



About Us    Contact Us    Privacy Policy    Terms Of Use