Click here to Skip to main content
Licence CPOL
First Posted 15 Oct 2008
Views 76,577
Downloads 4,210
Bookmarked 62 times

Excel export from DatagridView

By | 15 Oct 2008 | Article
A way to export data to native excel (xls) from a Datagridview with any data source

Introduction

A very common problem in winform applications occurs when data should be exported from DatagridView to Excel, there's some commercial workarounds to solve this problem. This article describe a simple versatile and free way to do.

Background  

The work to exporting to Excel is made by "Office XP Primary Interop Assemblies (PIAs)"

Initially, the data is obtained from datagridview's datasource, formated and then exported to a excel file throw Excel COM Interiop. 

The Datagridview datasource could be :

  • ArrayList of Entities 
  • Dataset with at least 1 datatable
  • Datatable

Data should be stored in a DataTable before beign exported to excel, if DataGridView's DataSource is an ArrayList, then the ArrayList is converted to a DataTable using the method ArrayListToDataTable

		ArrayList arr = (ArrayList)dataGridView.DataSource;
               dt = ArrayListToDataTable(arr);

 Once the data is stored in the datatable is exported to excel using the method dataTable2Excel

		dataTable2Excel(dt, dataGridView, pFullPath_toExport, nameSheet);

 

Using the code

You should only copy the following 2 files to your project 

  • ExportToExcel.cs
  • UC_DataGridView.cs
  • UC_DataGridView.Designer.cs

Use the DataGridView in you project and export to excel using the next code

Tools.ExportToExcel.ExportToExcel exp = new Tools.ExportToExcel.ExportToExcel(); 
exp.dataGridView2Excel(this.dgvDataToExport, path, "NameSheet"); 

Points of Interest 

Please note that the titles in excel file was obtained from DataGridView headers, not from DataTable name columns. 

There's a bug, titles of columns are not exported correct, i am working on it. 

History 

15/10/2008 First Release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

kevinuni

Software Developer
Promotora Opcion SA EAFC
Peru Peru

Member

Kevin Diaz is an electrical engineer student from UNI in Lima, Peru.
 
He work developing projects for financial and electrical sector.

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
GeneralMy vote of 4 Pinmemberyichangzyh0:26 4 Jan '12  
GeneralDatagrid jsp - Excel and PDF Pinmembervsuriyaprakash22:15 15 Feb '11  
GeneralMy vote of 2 Pinmemberlester5550:09 27 Nov '10  
QuestionIs there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberBigdeak4:45 14 Jun '10  
AnswerRe: Is there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberRyanALEX18:50 16 Aug '10  
AnswerRe: Is there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberCikaPero22:31 10 Apr '11  
QuestionWindows Vista problem ? PinmemberMember 152355914:24 21 Aug '09  
Generalwork with unicode(UT-8) Pinmembertruong21:04 17 Jun '09  
QuestionHow to use with MS Office 2003 PinmemberDelfy_Coltech4:14 24 Mar '09  
AnswerRe: How to use with MS Office 2003 Pinmembergg42372:22 30 Jul '09  
GeneralExcel saves, but no data Pinmemberbantar7616:10 15 Feb '09  
GeneralRe: Excel saves, but no data Pinmemberkevinuni16:21 15 Feb '09  
GeneralRe: Excel saves, but no data Pinmemberbantar7617:32 15 Feb '09  
GeneralRe: Excel saves, but no data Pinmemberbantar7616:10 16 Feb '09  
GeneralRe: Excel saves, but no data Pinmemberakarimov5:39 23 Nov '11  
Generalerror messeage. PinmemberMel Igo19:34 4 Jan '09  
GeneralRe: error messeage. Pinmemberkevinuni21:29 4 Jan '09  
GeneralHelp me! Pinmembertiencongaiocon22:58 11 Dec '08  
GeneralRe: Help me! Pinmemberkevinuni9:45 15 Dec '08  
Questionunicode support? PinmemberNg. Anh Vu5:38 22 Nov '08  
GeneralHi good work PinmemberMember 46241690:34 9 Nov '08  
GeneralRe: Hi good work Pinmemberkevinuni10:19 20 Nov '08  
GeneralRe: Hi good work Pinmemberkevinuni10:21 20 Nov '08  
GeneralHi Pinmembertaher khiari1:41 28 Oct '08  
GeneralRe: Hi Pinmemberkevinuni6:00 29 Oct '08  

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
Web01 | 2.5.120517.1 | Last Updated 15 Oct 2008
Article Copyright 2008 by kevinuni
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid