Click here to Skip to main content
11,409,181 members (51,280 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.


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);


  • 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.


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

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

GeneralMy vote of 4 Pinmemberkishore doni28-Aug-12 23:07 
QuestionHow I use this with a GridView and not a DataGrid? Pinmembercristinabarreira18-Nov-08 5:49 
AnswerRe: How I use this with a GridView and not a DataGrid? PinmemberCikaPero8-Sep-10 3:14 
QuestionDataGrid or List export to Excel in C# not ASP Pinmembermgruby29-Oct-08 1:53 
AnswerRe: DataGrid or List export to Excel in C# not ASP PinmemberCikaPero28-Feb-10 22:43 
AnswerRe: DataGrid or List export to Excel in C# not ASP Pinmemberlarno8-May-10 6:14 
GeneralExport DataGridview to Excel PinmemberDo viet Hai12-Jun-08 8:07 
GeneralAlternative way PinmemberBarbaMariolino16-May-08 4:48 
QuestionDoes it work with paging? Pinmemberiviruz.be7-May-08 0:52 
AnswerRe: Does it work with paging? PinmemberChris967519-May-08 16:22 
QuestionImport Datagrid to Excel Win application PinmemberWalaza16-Mar-08 8:15 
QuestionDoes Not work with third party Tools??? PinmemberKavitaAsnani8-Jan-08 1:45 
Questiondatagrid to excel Pinmembermahaguru1-Aug-07 23:51 
AnswerRe: datagrid to excel PinmemberRajeshcode12-Nov-07 20:35 
Generaldoesnt seem to work with forms Pinmemberjeweladdict14-Sep-06 9:27 
This only works with web datagrids?
GeneralRe: doesnt seem to work with forms PinmemberSam Slade25-Apr-07 1:36 
GeneralExport Infragettic DataGrid to Excel Pinmembertruongtx5-Jan-06 1:24 
Generaltrucating leading zeroes PinmemberEgghead wannabe9-Aug-05 7:23 
Generalexport to excel PinsussEma Mocanita1-Aug-05 23:47 
GeneralSelectedItem controls on datagrid Pinmemberptn7711-Apr-05 10:12 
QuestionWhat Goes Where? Pinmemberron e mtz7-Feb-05 10:20 
GeneralWin DataGrid Pinmemberjanigorse12-Jan-05 8:08 
Generalbig numbers are rounded PinsussAnonymous4-Jan-05 3:45 
GeneralNot Rendering my datagrid Pinmemberjken20308-Dec-04 7:00 
GeneralRe: Not Rendering my datagrid PinmemberErnieLynch26-Sep-07 10:03 
QuestionWhere is the MDF.CSS File??? Pinmembercerebraldisorder2-Dec-04 13:09 
AnswerRe: Where is the MDF.CSS File??? Pinmemberjken20308-Dec-04 6:58 
AnswerRe: Where is the MDF.CSS File??? PinmemberMax Black16-Feb-05 3:29 
AnswerRe: Where is the MDF.CSS File??? Pinmemberknarf_scot22-Jul-05 0:23 
GeneralProblem with Unicode !!! Pinmembertruonghongquan22-Nov-04 17:52 
GeneralRe: Problem with Unicode !!! Pinmembernutysy4-Feb-05 1:50 
GeneralRe: Problem with Unicode !!! Pinmemberoptyxaz14-Mar-06 1:45 
GeneralRe: Problem with Unicode !!! PinmemberMember 395040119-Sep-08 9:56 
GeneralVB.NET Code Pinmembernamaishi11-Nov-04 8:52 
GeneralRe: VB.NET Code Pinmemberhkamdar15-Feb-05 11:51 
Generalsome problem export DataGrid to excel Pinmemberkattaseshu915-Oct-04 1:50 
GeneralPaging PinmemberPraveenJhurani24-Sep-04 14:04 
GeneralRe: Paging Pinsusschris eklund31-Jan-05 9:07 
GeneralRe: Paging PinmemberP Prasad5-Dec-05 2:45 
GeneralDataGrid to Excel Pinmembermaximusus19-Sep-04 9:08 
GeneralThanks a ton!! PinmemberLaurieC17-Aug-04 10:31 PinsussAnonymous16-Jun-04 14:56 
GeneralRe: Pinmemberdezember4-May-06 1:37 
Generalsheet Pinmemberdevcat16-Jun-04 5:28 
GeneralComplete Working PinmemberBunts2-May-04 20:09 
GeneralHere Is How To Open(Save) In New Window PinmemberHushpappy11-Feb-04 22:20 
GeneralRe: Here Is How To Open(Save) In New Window Pinmemberjuantek28-Mar-04 21:58 
GeneralRe: Here Is How To Open(Save) In New Window PinmemberAFF9583314-Dec-04 13:22 
GeneralRe: Here Is How To Open(Save) In New Window PinmemberAFF9583314-Dec-04 13:19 
GeneralSpecial characters Pinmemberhorvat10-Feb-04 1:42 

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
Web01 | 2.8.150414.5 | Last Updated 20 Jun 2003
Article Copyright 2003 by Bhavtosh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid