Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

Export DataGrid to Excel

Rate me:
Please Sign up or sign in to vote.
3.23/5 (81 votes)
19 Jun 2003 382.1K   10K   66   71
Code that exports a whole DataGrid into an Excel file.

Introduction

This sample demonstrates "How to export the whole DataGrid into an Excel file".

If you are looking for an easy way to export your DataGrid into an Excel file, then you have the solution now.

HTML encode an entire DataGrid. The code iterates through each cell in the table row, ensuring that all the text being displayed is HTML encoded, irrespective of whether they are just plain text, buttons, hyperlinks, multiple controls etc.

Include this code in your application/project and use it anywhere you want. This code takes just one parameter, the DataGrid object itself. Here is a sample calling code to use this code:

C#
private void ExportLinkButton_Click(object sender, System.EventArgs e)
{
  string strTitle = "put the grid title here";
  new DataGridExcelExporter(this.YourGridName , this.Page).Export(strTitle);
}

where,

  • strTitle is the title of the DataGrid.
  • DataGridExcelExporter is the name of main class.
  • this.YourGridName is the DataGrid object.
  • this.Page is the current Page object.

All you need to pass is your DataGrid object and a sweet title for the grid, and rest is taken care by this code. After all, take a look at the code, it's pretty self explanatory.

Pre-requisite: Microsoft Excel 97 or above should be installed on the client machine in order to make this function work.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Hi all,

Im into software development for quite a long time now exclusively on Microsoft techologies.

At present im enjoying programming with .NET
Its just great !!!

C#,ASP.NET, Web Services, ADO.NET are my googles to search and explore.

When im not at work, i like listening music specaill love, rock, OSTs or going for a long drives!!!

Comments and Discussions

 
GeneralDoesn't work Pin
Member 8577292-Feb-04 13:44
Member 8577292-Feb-04 13:44 
GeneralRe: Doesn't work Pin
jasmine2210-Jun-04 4:41
jasmine2210-Jun-04 4:41 
GeneralRe: Doesn't work Pin
truonghongquan22-Nov-04 16:45
truonghongquan22-Nov-04 16:45 
Generalopen a new window Pin
Member 48522223-Jul-03 1:07
Member 48522223-Jul-03 1:07 
GeneralLooking 4 same thing Pin
Rescue_Ranger19-Jul-03 11:36
Rescue_Ranger19-Jul-03 11:36 
GeneralRe: Looking 4 same thing Pin
eddididi27-Aug-03 0:01
eddididi27-Aug-03 0:01 
GeneralWhat to do? Pin
Rescue_Ranger1-Sep-03 9:10
Rescue_Ranger1-Sep-03 9:10 
Generalfrom the bottom: It's very strange that you got first 5 because F*** YOU Pin
doom12345678920-Jun-03 4:56
doom12345678920-Jun-03 4:56 
Mad | :mad: Mad | :mad: Mad | :mad:
Generalwhy no even one line of code Pin
doom12345678920-Jun-03 2:32
doom12345678920-Jun-03 2:32 
GeneralNice but... Pin
Steve McLenithan20-Jun-03 1:54
Steve McLenithan20-Jun-03 1:54 
GeneralRe: Nice but... Pin
Vladimir Afanasyev20-Jun-03 3:04
Vladimir Afanasyev20-Jun-03 3:04 
GeneralRe: Nice but... Pin
Steve McLenithan20-Jun-03 3:08
Steve McLenithan20-Jun-03 3:08 
GeneralRe: Nice but... Pin
Vladimir Afanasyev20-Jun-03 3:20
Vladimir Afanasyev20-Jun-03 3:20 
GeneralAnd where the download Pin
Vladimir Afanasyev19-Jun-03 21:45
Vladimir Afanasyev19-Jun-03 21:45 
GeneralRe: And where the download Pin
doom12345678920-Jun-03 2:34
doom12345678920-Jun-03 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.