Click here to Skip to main content
Licence 
First Posted 19 Jun 2003
Views 290,165
Downloads 6,554
Bookmarked 61 times

Export DataGrid to Excel

By Bhavtosh | 19 Jun 2003
Code that exports a whole DataGrid into an Excel file.
31 votes, 40.3%
1
6 votes, 7.8%
2
5 votes, 6.5%
3
5 votes, 6.5%
4
30 votes, 39.0%
5
3.17/5 - 77 votes
μ 2.97, σa 3.20 [?]

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

About the Author

Bhavtosh

Web Developer

United States United States

Member
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!!!


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow I use this with a GridView and not a DataGrid? Pinmembercristinabarreira5:49 18 Nov '08  
AnswerRe: How I use this with a GridView and not a DataGrid? PinmemberCikaPero3:14 8 Sep '10  
QuestionDataGrid or List export to Excel in C# not ASP Pinmembermgruby1:53 29 Oct '08  
AnswerRe: DataGrid or List export to Excel in C# not ASP PinmemberCikaPero22:43 28 Feb '10  
AnswerRe: DataGrid or List export to Excel in C# not ASP Pinmemberlarno6:14 8 May '10  
GeneralExport DataGridview to Excel PinmemberDo viet Hai8:07 12 Jun '08  
GeneralAlternative way PinmemberBarbaMariolino4:48 16 May '08  
QuestionDoes it work with paging? Pinmemberiviruz.be0:52 7 May '08  
AnswerRe: Does it work with paging? PinmemberChris967516:22 19 May '08  
QuestionImport Datagrid to Excel Win application PinmemberWalaza8:15 16 Mar '08  
QuestionDoes Not work with third party Tools??? PinmemberKavitaAsnani1:45 8 Jan '08  
Questiondatagrid to excel Pinmembermahaguru23:51 1 Aug '07  
AnswerRe: datagrid to excel PinmemberRajeshcode20:35 12 Nov '07  
Generaldoesnt seem to work with forms Pinmemberjeweladdict9:27 14 Sep '06  
GeneralRe: doesnt seem to work with forms PinmemberSam Slade1:36 25 Apr '07  
GeneralExport Infragettic DataGrid to Excel Pinmembertruongtx1:24 5 Jan '06  
Generaltrucating leading zeroes PinmemberEgghead wannabe7:23 9 Aug '05  
Generalexport to excel PinsussEma Mocanita23:47 1 Aug '05  
GeneralSelectedItem controls on datagrid Pinmemberptn7710:12 11 Apr '05  
QuestionWhat Goes Where? Pinmemberron e mtz10:20 7 Feb '05  
GeneralWin DataGrid Pinmemberjanigorse8:08 12 Jan '05  
Generalbig numbers are rounded PinsussAnonymous3:45 4 Jan '05  
GeneralNot Rendering my datagrid Pinmemberjken20307:00 8 Dec '04  
GeneralRe: Not Rendering my datagrid PinmemberErnieLynch10:03 26 Sep '07  
QuestionWhere is the MDF.CSS File??? Pinmembercerebraldisorder13:09 2 Dec '04  

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.

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