Click here to Skip to main content
15,498,859 members
Articles / Programming Languages / Visual Basic
Posted 1 Dec 2007


98 bookmarked

Fast Exporting from DataSet to Excel

Rate me:
Please Sign up or sign in to vote.
4.70/5 (54 votes)
1 Dec 2007CPOL2 min read
A fast method for exporting large amounts of data from a DataSet to Excel
Screenshot - FastExporting DEMO App


Exporting data from a .NET application to Excel is a very common requirement. A simple search on the Web results in several examples that show us the method to copy data and put it into the Excel cells. However, there is a payload with this method: each interaction to put a value into an Excel cell requires an InterOp invocation. If the amount of data to transfer is huge, we have a problem with the performance of the method. Is there a better way to accomplish this?

Traditional "COPY CELL-BY-CELL" Method

Searching the Web for a method to transfer data to Excel, the most commonly used method consists of copying the values cell by cell into Excel. The following C# code shows how to transfer data from a DataTable to an Excel sheet, copying each value cell by cell:

// Copy the values from a DataTable to an Excel Sheet (cell-by-cell)
for (int col = 0; col < dataTable.Columns.Count; col++)
    for (int row = 0; row < dataTable.Rows.Count; row++)
        excelSheet.Cells[row + 1, col + 1] = 

Each InterOp invocation has an associated payload in performance, so a large amount of data can degenerate our application.

A "Fast Bulk-Copy" Method

Our method consists of using the Value2 property for the Range class provided by the Microsoft Excel Object Library. We can select a range of cells, and assign a value for all of them, with just one InterOp invocation. To correctly assign a value to a range of cells, we can use a bi-dimensional object array. The following C# code shows how to transfer data from a bi-dimensional object array to a range of cells:

// Copy a bi-dimensional object array to an Excel cell range
excelSheet.get_Range("A1:H25", Type.Missing).Value2 = 

Measuring the Performance

The source code included with this article shows a small Windows application which uses the two described methods to export the same data to Excel. It shows the time that it takes for each method to finish. This DEMO uses the Northwind database to create an SQL Server local connection. It generates a DataSet with the content of the Customers table. To make the amount of data more significant, we duplicate the DataTable to obtain 24 copies from it. Then we apply the two methods to generate two Excel books, one for each method.

The source code includes a C# and a VB.NET version for the DEMO application. My own testing shows me that this method is about 35 times faster. Test it and arrive at your own conclusions.


  • November 28, 2007: First publication


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

Written By
Software Developer (Senior)
Ecuador Ecuador
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questiondatabase customer is not in sourcecode project Pin
vijay140718-Jan-19 5:43
Membervijay140718-Jan-19 5:43 
QuestionImplementing a Progress Bar Pin
Alec Angelo Simoy12-Dec-18 16:57
MemberAlec Angelo Simoy12-Dec-18 16:57 
QuestionThanks a TONNN!!!! Pin
Member 1402777721-Oct-18 22:48
MemberMember 1402777721-Oct-18 22:48 
QuestionIt's great. Tks you so much Pin
Dat Tran12-Aug-18 20:07
MemberDat Tran12-Aug-18 20:07 
QuestionBeautiful Pin
oceanmaui4-Feb-18 16:27
Memberoceanmaui4-Feb-18 16:27 
Questiondatetime convertin Pin
sirboy7-Jan-17 5:37
Membersirboy7-Jan-17 5:37 
QuestionThanks Pin
kuntalbd22-Jun-16 21:31
Memberkuntalbd22-Jun-16 21:31 
SuggestionThere also another efficient way of exporting data to excel Pin
Senthil Sambandam14-Nov-14 17:50
professionalSenthil Sambandam14-Nov-14 17:50 
Questionsolution for this? Pin
NekoNao21-Sep-14 21:01
MemberNekoNao21-Sep-14 21:01 
GeneralAmazing Pin
RickoT26-Aug-14 13:21
MemberRickoT26-Aug-14 13:21 
QuestionSee also this: Pin
dietmar paul schoder29-Jul-14 6:23
professionaldietmar paul schoder29-Jul-14 6:23 
Member 1055846615-Apr-14 6:39
MemberMember 1055846615-Apr-14 6:39 
jfigueroa212-Apr-14 9:04
Memberjfigueroa212-Apr-14 9:04 
QuestionHi Pin
Mylog.2522-Feb-14 23:53
MemberMylog.2522-Feb-14 23:53 
GeneralMy vote of 5 Pin
rensq880829-Aug-13 21:06
Memberrensq880829-Aug-13 21:06 
GeneralThank you! Pin
Barbara_IT_6828-Jul-13 2:47
MemberBarbara_IT_6828-Jul-13 2:47 
QuestionGreat !!! Fastest !!! Pin
nhwaynhwaycho20-Jun-13 18:03
Membernhwaynhwaycho20-Jun-13 18:03 
QuestionSUPER FAST. Pin
AliN Indrei29-Apr-13 10:52
MemberAliN Indrei29-Apr-13 10:52 
QuestionGREAT! Pin
Daniel Carvalho Liedke17-Aug-12 8:28
MemberDaniel Carvalho Liedke17-Aug-12 8:28 
GeneralGreat Project Pin
we6art19-Apr-12 2:44
Memberwe6art19-Apr-12 2:44 
Suggestiona close-safe approach [arranged code] Pin
Arnaud Dovi6-Mar-12 6:11
MemberArnaud Dovi6-Mar-12 6:11 
GeneralRe: a close-safe approach [arranged code] Pin
bgsjust11-Jun-13 15:07
professionalbgsjust11-Jun-13 15:07 
GeneralMy vote of 5 Pin
JohnCollett14-Jan-12 6:02
MemberJohnCollett14-Jan-12 6:02 
GeneralMy vote of 5 Pin
Oshtri Deka21-Oct-11 5:24
professionalOshtri Deka21-Oct-11 5:24 
QuestionMy vote of 5 Pin
paolo.balbarini23-Sep-11 4:33
Memberpaolo.balbarini23-Sep-11 4:33 

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.