Click here to Skip to main content
15,861,125 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 382K   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

 
QuestionIs there any license info or restrictions for use of this code I would like to use it? Pin
cwfontan1-Mar-16 10:47
cwfontan1-Mar-16 10:47 
GeneralMy vote of 4 Pin
D-Kishore28-Aug-12 22:07
D-Kishore28-Aug-12 22:07 
QuestionHow I use this with a GridView and not a DataGrid? Pin
cristinabarreira18-Nov-08 4:49
cristinabarreira18-Nov-08 4:49 
AnswerRe: How I use this with a GridView and not a DataGrid? Pin
CikaPero8-Sep-10 2:14
CikaPero8-Sep-10 2:14 
Hi,

using this Excel .NET component you can export GridView to Excel.

Here is an Excel VB.NET sample how to do this from your Excel ASP.NET project:

' Get DataTable that GridView is bound to.
Dim dataTable = DirectCast(gridView.DataSource, DataTable)

' Create new ExcelFile.
Dim ef = New ExcelFile()
' Add new worksheet to the file.
Dim ws = ef.Worksheets.Add(dataTable.TableName)
' Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", True)

' Stream file to browser.
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls")
ef.SaveXls(Response.OutputStream)
Response.[End]()

QuestionDataGrid or List export to Excel in C# not ASP Pin
mgruby29-Oct-08 0:53
mgruby29-Oct-08 0:53 
AnswerRe: DataGrid or List export to Excel in C# not ASP Pin
CikaPero28-Feb-10 21:43
CikaPero28-Feb-10 21:43 
AnswerRe: DataGrid or List export to Excel in C# not ASP Pin
larno8-May-10 5:14
larno8-May-10 5:14 
GeneralExport DataGridview to Excel Pin
Do viet Hai12-Jun-08 7:07
Do viet Hai12-Jun-08 7:07 
GeneralAlternative way Pin
FilipKrnjic16-May-08 3:48
FilipKrnjic16-May-08 3:48 
QuestionDoes it work with paging? Pin
iviruz.be6-May-08 23:52
iviruz.be6-May-08 23:52 
AnswerRe: Does it work with paging? Pin
Chris967519-May-08 15:22
Chris967519-May-08 15:22 
QuestionImport Datagrid to Excel Win application Pin
Walaza16-Mar-08 7:15
Walaza16-Mar-08 7:15 
QuestionDoes Not work with third party Tools??? Pin
KavitaAsnani8-Jan-08 0:45
KavitaAsnani8-Jan-08 0:45 
Questiondatagrid to excel Pin
mahaguru1-Aug-07 22:51
mahaguru1-Aug-07 22:51 
AnswerRe: datagrid to excel Pin
Rajeshcode12-Nov-07 19:35
Rajeshcode12-Nov-07 19:35 
Generaldoesnt seem to work with forms Pin
jeweladdict14-Sep-06 8:27
jeweladdict14-Sep-06 8:27 
GeneralRe: doesnt seem to work with forms Pin
Sam Slade25-Apr-07 0:36
Sam Slade25-Apr-07 0:36 
GeneralExport Infragettic DataGrid to Excel Pin
truongtx5-Jan-06 0:24
truongtx5-Jan-06 0:24 
Generaltrucating leading zeroes Pin
Dennis20079-Aug-05 6:23
Dennis20079-Aug-05 6:23 
Generalexport to excel Pin
emamocanita1-Aug-05 22:47
emamocanita1-Aug-05 22:47 
GeneralSelectedItem controls on datagrid Pin
ptn7711-Apr-05 9:12
ptn7711-Apr-05 9:12 
QuestionWhat Goes Where? Pin
ron e mtz7-Feb-05 9:20
ron e mtz7-Feb-05 9:20 
GeneralWin DataGrid Pin
janigorse12-Jan-05 7:08
janigorse12-Jan-05 7:08 
Generalbig numbers are rounded Pin
Anonymous4-Jan-05 2:45
Anonymous4-Jan-05 2:45 
GeneralNot Rendering my datagrid Pin
jken20308-Dec-04 6:00
jken20308-Dec-04 6:00 

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.