Click here to Skip to main content
11,496,146 members (1,077 online)
Click here to Skip to main content

Excel export from DatagridView

, 15 Oct 2008 CPOL 106K 8.6K 72
A way to export data to native excel (xls) from a Datagridview with any data source
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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)

Share

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

 
QuestionHow to use with MS Office 2003 Pin
Delfy_Coltech24-Mar-09 5:14
memberDelfy_Coltech24-Mar-09 5:14 
AnswerRe: How to use with MS Office 2003 Pin
gg423730-Jul-09 3:22
membergg423730-Jul-09 3:22 

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.150520.1 | Last Updated 15 Oct 2008
Article Copyright 2008 by kevinuni
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid