Click here to Skip to main content
Click here to Skip to main content

Excel export from DatagridView

By , 15 Oct 2008
Rate this:
Please Sign up or sign in to vote.

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
Kevin Diaz is an electrical engineer student from UNI in Lima, Peru.
 
He work developing projects for financial and electrical sector.

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberyichangzyh4-Jan-12 0:26 
GeneralDatagrid jsp - Excel and PDF Pinmembervsuriyaprakash15-Feb-11 22:15 
GeneralMy vote of 2 Pinmemberlester55527-Nov-10 0:09 
QuestionIs there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberBigdeak14-Jun-10 4:45 
AnswerRe: Is there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberRyanALEX16-Aug-10 18:50 
AnswerRe: Is there a way to Export it without using the Microsoft.Office.Interop.Excel.dll and Microsoft.Office.Interop.Word.dll? PinmemberCikaPero10-Apr-11 22:31 
QuestionWindows Vista problem ? PinmemberMember 152355921-Aug-09 14:24 
Generalwork with unicode(UT-8) Pinmembertruong17-Jun-09 21:04 
when the switch to unicode(UT-8) font fail
Sigh | :sigh:
QuestionHow to use with MS Office 2003 PinmemberDelfy_Coltech24-Mar-09 4:14 
AnswerRe: How to use with MS Office 2003 Pinmembergg423730-Jul-09 2:22 
GeneralExcel saves, but no data Pinmemberbantar7615-Feb-09 16:10 
GeneralRe: Excel saves, but no data Pinmemberkevinuni15-Feb-09 16:21 
GeneralRe: Excel saves, but no data Pinmemberbantar7615-Feb-09 17:32 
GeneralRe: Excel saves, but no data Pinmemberbantar7616-Feb-09 16:10 
GeneralRe: Excel saves, but no data Pinmemberakarimov23-Nov-11 5:39 
Generalerror messeage. PinmemberMel Igo4-Jan-09 19:34 
GeneralRe: error messeage. Pinmemberkevinuni4-Jan-09 21:29 
GeneralHelp me! Pinmembertiencongaiocon11-Dec-08 22:58 
GeneralRe: Help me! Pinmemberkevinuni15-Dec-08 9:45 
Questionunicode support? PinmemberNg. Anh Vu22-Nov-08 5:38 
GeneralHi good work PinmemberMember 46241699-Nov-08 0:34 
GeneralRe: Hi good work Pinmemberkevinuni20-Nov-08 10:19 
GeneralRe: Hi good work Pinmemberkevinuni20-Nov-08 10:21 
GeneralHi Pinmembertaher khiari28-Oct-08 1:41 
GeneralRe: Hi Pinmemberkevinuni29-Oct-08 6:00 

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