Click here to Skip to main content
6,821,293 members and growing! (19,155 online)
Email Password   helpLost your password?
Languages » C# » Samples     Intermediate

Export DataGrid to Excel

By Bhavtosh

Code that exports a whole DataGrid into an Excel file.
C#, VC7.NET1.0, Win2K, WinXP, Win2003, ASP.NET, Dev
Posted:19 Jun 2003
Views:235,655
Bookmarked:53 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
76 votes for this article.
Popularity: 5.55 Rating: 2.95 out of 5
31 votes, 40.8%
1
6 votes, 7.9%
2
5 votes, 6.6%
3
5 votes, 6.6%
4
29 votes, 38.2%
5

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


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

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 66 (Total in Forum: 66) (Refresh)FirstPrevNext
GeneralHow I use this with a GridView and not a DataGrid? Pinmembercristinabarreira5:49 18 Nov '08  
QuestionDataGrid or List export to Excel in C# not ASP Pinmembermgruby1:53 29 Oct '08  
GeneralExport DataGridview to Excel PinmemberDo viet Hai8:07 12 Jun '08  
GeneralAlternative way PinmemberBarbaMariolino4:48 16 May '08  
GeneralDoes it work with paging? Pinmemberiviruz.be0:52 7 May '08  
GeneralRe: Does it work with paging? PinmemberChris967516:22 19 May '08  
QuestionImport Datagrid to Excel Win application PinmemberWalaza8:15 16 Mar '08  
GeneralDoes 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  
GeneralWhat 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  
GeneralWhere is the MDF.CSS File??? Pinmembercerebraldisorder13:09 2 Dec '04  
GeneralRe: Where is the MDF.CSS File??? Pinmemberjken20306:58 8 Dec '04  
GeneralRe: Where is the MDF.CSS File??? PinmemberMax Black3:29 16 Feb '05  
GeneralRe: Where is the MDF.CSS File??? Pinmemberknarf_scot0:23 22 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jun 2003
Editor: Smitha Vijayan
Copyright 2003 by Bhavtosh
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project