C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Export GridView to Excel File using Asp.net 2.0


Posted Date: 19 Aug 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: Nitin Kumar Sharma       Member Level: Silver
Rating:     Points: 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


No responses found. Be the first to respond and make money from revenue sharing program.

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

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 Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween mask

Contact Us    Privacy Policy    Terms Of Use