Click here to Skip to main content
15,881,838 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.6K   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

 
GeneralRe: Problem with Unicode !!! Pin
optyxaz14-Mar-06 0:45
optyxaz14-Mar-06 0:45 
GeneralRe: Problem with Unicode !!! Pin
Member 395040119-Sep-08 8:56
Member 395040119-Sep-08 8:56 
GeneralVB.NET Code Pin
namaishi11-Nov-04 7:52
namaishi11-Nov-04 7:52 
GeneralRe: VB.NET Code Pin
hkamdar15-Feb-05 10:51
hkamdar15-Feb-05 10:51 
Generalsome problem export DataGrid to excel Pin
kattaseshu915-Oct-04 0:50
kattaseshu915-Oct-04 0:50 
GeneralPaging Pin
PraveenJhurani24-Sep-04 13:04
PraveenJhurani24-Sep-04 13:04 
GeneralRe: Paging Pin
ceklund31-Jan-05 8:07
ceklund31-Jan-05 8:07 
GeneralRe: Paging Pin
P Prasad5-Dec-05 1:45
P Prasad5-Dec-05 1:45 
DataSet Property AllowPaging can set the true or false.
GeneralDataGrid to Excel Pin
maximusus19-Sep-04 8:08
maximusus19-Sep-04 8:08 
GeneralThanks a ton!! Pin
LaurieC17-Aug-04 9:31
LaurieC17-Aug-04 9:31 
Generalthis.page Pin
Anonymous16-Jun-04 13:56
Anonymous16-Jun-04 13:56 
GeneralRe: this.page Pin
dezember4-May-06 0:37
dezember4-May-06 0:37 
Generalsheet Pin
devcat16-Jun-04 4:28
devcat16-Jun-04 4:28 
GeneralComplete Working Pin
Bunts2-May-04 19:09
Bunts2-May-04 19:09 
GeneralHere Is How To Open(Save) In New Window Pin
Hushpappy11-Feb-04 21:20
Hushpappy11-Feb-04 21:20 
GeneralRe: Here Is How To Open(Save) In New Window Pin
juantek28-Mar-04 20:58
juantek28-Mar-04 20:58 
GeneralRe: Here Is How To Open(Save) In New Window Pin
AFF9583314-Dec-04 12:22
AFF9583314-Dec-04 12:22 
GeneralRe: Here Is How To Open(Save) In New Window Pin
AFF9583314-Dec-04 12:19
AFF9583314-Dec-04 12:19 
GeneralSpecial characters Pin
horvat10-Feb-04 0:42
horvat10-Feb-04 0:42 
GeneralRe: Special characters Pin
Chad™3-Oct-04 15:30
sussChad™3-Oct-04 15:30 
GeneralRe: Special characters Pin
Anonymous27-Apr-05 0:55
Anonymous27-Apr-05 0:55 
GeneralRe: Special characters Pin
Roger Willcocks7-Jul-05 14:12
Roger Willcocks7-Jul-05 14:12 
GeneralGreat, but .. (new window) Pin
Hushpappy8-Feb-04 23:01
Hushpappy8-Feb-04 23:01 
Generalworks quite well Pin
codebrew5-Feb-04 9:38
codebrew5-Feb-04 9:38 
GeneralDoesn't work Pin
Member 8577292-Feb-04 13:44
Member 8577292-Feb-04 13:44 

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.