Click here to Skip to main content
13,558,244 members
Click here to Skip to main content
Add your own
alternative version


74 bookmarked
Posted 15 Oct 2008
Licenced CPOL

Excel export from DatagridView

, 15 Oct 2008
Rate this:
Please Sign up or sign in to vote.
A way to export data to native excel (xls) from a Datagridview with any data source


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.


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. 


15/10/2008 First Release


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


About the Author

Software Developer
Peru Peru
Kevin Diaz is an electrical engineer by UNI in Lima, Peru.

He work developing projects for electrical industry.

You may also be interested in...

Comments and Discussions

GeneralExcel saves, but no data Pin
bantar7615-Feb-09 16:10
memberbantar7615-Feb-09 16:10 
GeneralRe: Excel saves, but no data Pin
kevinuni15-Feb-09 16:21
memberkevinuni15-Feb-09 16:21 
GeneralRe: Excel saves, but no data Pin
bantar7615-Feb-09 17:32
memberbantar7615-Feb-09 17:32 
GeneralRe: Excel saves, but no data Pin
bantar7616-Feb-09 16:10
memberbantar7616-Feb-09 16:10 
GeneralRe: Excel saves, but no data Pin
akarimov23-Nov-11 5:39
memberakarimov23-Nov-11 5:39 
Generalerror messeage. Pin
Mel Igo4-Jan-09 19:34
memberMel Igo4-Jan-09 19:34 
GeneralRe: error messeage. Pin
kevinuni4-Jan-09 21:29
memberkevinuni4-Jan-09 21:29 
GeneralHelp me! Pin
tiencongaiocon11-Dec-08 22:58
membertiencongaiocon11-Dec-08 22:58 
GeneralRe: Help me! Pin
kevinuni15-Dec-08 9:45
memberkevinuni15-Dec-08 9:45 
Questionunicode support? Pin
Ng. Anh Vu22-Nov-08 5:38
memberNg. Anh Vu22-Nov-08 5:38 
GeneralHi good work Pin
Member 46241699-Nov-08 0:34
memberMember 46241699-Nov-08 0:34 
GeneralRe: Hi good work Pin
kevinuni20-Nov-08 10:19
memberkevinuni20-Nov-08 10:19 
GeneralRe: Hi good work Pin
kevinuni20-Nov-08 10:21
memberkevinuni20-Nov-08 10:21 
GeneralHi Pin
taher khiari28-Oct-08 1:41
membertaher khiari28-Oct-08 1:41 
GeneralRe: Hi Pin
kevinuni29-Oct-08 6:00
memberkevinuni29-Oct-08 6:00 
GeneralYou've cut my work time in half! Pin
Brent Maxwell23-Oct-08 14:19
memberBrent Maxwell23-Oct-08 14:19 
GeneralRe: You've cut my work time in half! Pin
kevinuni23-Oct-08 15:10
memberkevinuni23-Oct-08 15:10 
GeneralOther Way ADO Pin
Neil de Weerdt15-Oct-08 22:50
memberNeil de Weerdt15-Oct-08 22:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 15 Oct 2008
Article Copyright 2008 by kevinuni
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid