Click here to Skip to main content
11,433,997 members (55,911 online)
Click here to Skip to main content

Export DataGrid to Excel

, 19 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

Bhavtosh
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

 
QuestionWhere is the MDF.CSS File??? Pin
cerebraldisorder2-Dec-04 13:09
membercerebraldisorder2-Dec-04 13:09 
AnswerRe: Where is the MDF.CSS File??? Pin
jken20308-Dec-04 6:58
memberjken20308-Dec-04 6:58 
AnswerRe: Where is the MDF.CSS File??? Pin
Max Black16-Feb-05 3:29
memberMax Black16-Feb-05 3:29 
AnswerRe: Where is the MDF.CSS File??? Pin
knarf_scot22-Jul-05 0:23
memberknarf_scot22-Jul-05 0:23 
GeneralProblem with Unicode !!! Pin
truonghongquan22-Nov-04 17:52
membertruonghongquan22-Nov-04 17:52 
GeneralRe: Problem with Unicode !!! Pin
nutysy4-Feb-05 1:50
membernutysy4-Feb-05 1:50 
GeneralRe: Problem with Unicode !!! Pin
optyxaz14-Mar-06 1:45
memberoptyxaz14-Mar-06 1:45 
GeneralRe: Problem with Unicode !!! Pin
Member 395040119-Sep-08 9:56
memberMember 395040119-Sep-08 9:56 
GeneralVB.NET Code Pin
namaishi11-Nov-04 8:52
membernamaishi11-Nov-04 8:52 
GeneralRe: VB.NET Code Pin
hkamdar15-Feb-05 11:51
memberhkamdar15-Feb-05 11:51 
Generalsome problem export DataGrid to excel Pin
kattaseshu915-Oct-04 1:50
memberkattaseshu915-Oct-04 1:50 
GeneralPaging Pin
PraveenJhurani24-Sep-04 14:04
memberPraveenJhurani24-Sep-04 14:04 
GeneralRe: Paging Pin
chris eklund31-Jan-05 9:07
susschris eklund31-Jan-05 9:07 
GeneralRe: Paging Pin
P Prasad5-Dec-05 2:45
memberP Prasad5-Dec-05 2:45 
GeneralDataGrid to Excel Pin
maximusus19-Sep-04 9:08
membermaximusus19-Sep-04 9:08 
GeneralThanks a ton!! Pin
LaurieC17-Aug-04 10:31
memberLaurieC17-Aug-04 10:31 
Generalthis.page Pin
Anonymous16-Jun-04 14:56
sussAnonymous16-Jun-04 14:56 
GeneralRe: this.page Pin
dezember4-May-06 1:37
memberdezember4-May-06 1:37 
Generalsheet Pin
devcat16-Jun-04 5:28
memberdevcat16-Jun-04 5:28 
GeneralComplete Working Pin
Bunts2-May-04 20:09
memberBunts2-May-04 20:09 
GeneralHere Is How To Open(Save) In New Window Pin
Hushpappy11-Feb-04 22:20
memberHushpappy11-Feb-04 22:20 
GeneralRe: Here Is How To Open(Save) In New Window Pin
juantek28-Mar-04 21:58
memberjuantek28-Mar-04 21:58 
GeneralRe: Here Is How To Open(Save) In New Window Pin
AFF9583314-Dec-04 13:22
memberAFF9583314-Dec-04 13:22 
GeneralRe: Here Is How To Open(Save) In New Window Pin
AFF9583314-Dec-04 13:19
memberAFF9583314-Dec-04 13:19 
GeneralSpecial characters Pin
horvat10-Feb-04 1:42
memberhorvat10-Feb-04 1:42 
GeneralRe: Special characters Pin
Chad™3-Oct-04 16:30
sussChad™3-Oct-04 16:30 
GeneralRe: Special characters Pin
Anonymous27-Apr-05 1:55
sussAnonymous27-Apr-05 1:55 
GeneralRe: Special characters Pin
Roger Willcocks7-Jul-05 15:12
memberRoger Willcocks7-Jul-05 15:12 
GeneralGreat, but .. (new window) Pin
Hushpappy9-Feb-04 0:01
memberHushpappy9-Feb-04 0:01 
Generalworks quite well Pin
codebrew5-Feb-04 10:38
susscodebrew5-Feb-04 10:38 
GeneralDoesn't work Pin
Bapudi2-Feb-04 14:44
memberBapudi2-Feb-04 14:44 
GeneralRe: Doesn't work Pin
jasmine2210-Jun-04 5:41
memberjasmine2210-Jun-04 5:41 
GeneralRe: Doesn't work Pin
truonghongquan22-Nov-04 17:45
membertruonghongquan22-Nov-04 17:45 
Generalopen a new window Pin
jamal_fakhru@hotmail.com23-Jul-03 2:07
memberjamal_fakhru@hotmail.com23-Jul-03 2:07 
GeneralLooking 4 same thing Pin
Rescue_Ranger19-Jul-03 12:36
memberRescue_Ranger19-Jul-03 12:36 
GeneralRe: Looking 4 same thing Pin
eddididi27-Aug-03 1:01
membereddididi27-Aug-03 1:01 
GeneralWhat to do? Pin
Rescue_Ranger1-Sep-03 10:10
memberRescue_Ranger1-Sep-03 10:10 
Generalfrom the bottom: It's very strange that you got first 5 because f*** YOU Pin
doom12345678920-Jun-03 5:56
memberdoom12345678920-Jun-03 5:56 
Generalwhy no even one line of code Pin
doom12345678920-Jun-03 3:32
memberdoom12345678920-Jun-03 3:32 
GeneralNice but... Pin
Steve McLenithan20-Jun-03 2:54
memberSteve McLenithan20-Jun-03 2:54 
GeneralRe: Nice but... Pin
Vladimir Afanasyev20-Jun-03 4:04
memberVladimir Afanasyev20-Jun-03 4:04 
GeneralRe: Nice but... Pin
Steve McLenithan20-Jun-03 4:08
memberSteve McLenithan20-Jun-03 4:08 
GeneralRe: Nice but... Pin
Vladimir Afanasyev20-Jun-03 4:20
memberVladimir Afanasyev20-Jun-03 4:20 
GeneralAnd where the download Pin
Vladimir Afanasyev19-Jun-03 22:45
memberVladimir Afanasyev19-Jun-03 22:45 
GeneralRe: And where the download Pin
doom12345678920-Jun-03 3:34
memberdoom12345678920-Jun-03 3:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 20 Jun 2003
Article Copyright 2003 by Bhavtosh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid