Click here to Skip to main content
Click here to Skip to main content
Go to top

Fast Exporting from DataSet to Excel

, 1 Dec 2007
Rate this:
Please Sign up or sign in to vote.
A fast method for exporting large amounts of data from a DataSet to Excel
Screenshot - FastExporting DEMO App

Introduction

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] = 
                dataTable.Rows[row].ItemArray[col];
    }
}

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 = 
    bidimensionalObjectArray;

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.

History

  • November 28, 2007: First publication

License

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

Share

About the Author

PeterMoon
Software Developer (Senior)
Ecuador Ecuador
No Biography provided

Comments and Discussions

 
Questionsolution for this? PinmemberNekoNao21-Sep-14 20:01 
GeneralAmazing PinmemberRickoT26-Aug-14 12:21 
QuestionSee also this: PinprofessionalDietmar Schoder29-Jul-14 5:23 
QuestionREALLY FREAKING FAST THANKS SO MUCH! PinmemberMember 1055846615-Apr-14 5:39 
QuestionJFIGUEROA Pinmemberjfigueroa212-Apr-14 8:04 
QuestionHi PinmemberMylog.2522-Feb-14 22:53 
GeneralMy vote of 5 Pinmemberrensq880829-Aug-13 20:06 
GeneralThank you! PinmemberBarbara_IT_6828-Jul-13 1:47 
QuestionGreat !!! Fastest !!! Pinmembernhwaynhwaycho20-Jun-13 17:03 
QuestionSUPER FAST. PinmemberAliN Indrei29-Apr-13 9:52 
QuestionGREAT! PinmemberDaniel Carvalho Liedke17-Aug-12 7:28 
GeneralGreat Project Pinmemberwe6art19-Apr-12 1:44 
Suggestiona close-safe approach [arranged code] [modified] PinmemberArnaud Dovi6-Mar-12 5:11 
GeneralRe: a close-safe approach [arranged code] Pinmemberbgsjust11-Jun-13 14:07 
GeneralMy vote of 5 PinmemberJohnCollett14-Jan-12 5:02 
GeneralMy vote of 5 PinmemberOshtri Deka21-Oct-11 4:24 
QuestionMy vote of 5 Pinmemberpaolo.balbarini23-Sep-11 3:33 
QuestionAlternative PinmemberCikaPero24-Jul-11 20:51 
AnswerRe: Alternative Pinmemberadilson2105-Jan-12 14:10 
GeneralMy vote of 5 Pinmemberdharaneendra13-Mar-11 21:34 
Generalgetting stuck at excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData; Pinmemberjohn_17264-Jan-11 10:15 
GeneralMy vote of 5 PinmemberStefano Manni15-Oct-10 1:18 
GeneralExcelente ejemplo! Pinmembervicentehg30-Sep-10 8:55 
GeneralTIP: Cell value starts with equal symbol PinmemberGQ201015-Sep-10 12:40 
GeneralMy vote of 5 PinmemberGQ201015-Sep-10 12:32 
QuestionWhy it cant supports Excel 2007 format (xls)? Pinmemberjackkitcc15-Sep-10 0:57 
GeneralVery Fast! PinmemberHelen Goussarova7-Jul-10 2:15 
GeneralThank you for the cool trick. PinmemberCaipus7-Jun-10 4:09 
Generalsuperb code. PinmemberAbithaSarma3-Jun-10 7:21 
Questionhow to get the pop up window as "Open" "Save" "Cancel" PinmemberDarshan M R13-Oct-09 4:41 
General[Error] Date Time fields are coming as Numbers. Pinmemberjak01018-Jun-09 1:06 
GeneralRe: [Error] Date Time fields are coming as Numbers. PinmemberPeterMoon8-Jun-09 14:36 
GeneralRe: [Error] Date Time fields are coming as Numbers. Pinmemberjak010110-Jun-09 19:50 
QuestionRe: [Error] Date Time fields are coming as Numbers. [modified] PinmemberSteven Bell7-Oct-09 18:25 
GeneralRe: [Error] Date Time fields are coming as Numbers. Pinmemberkuklei16-Dec-10 3:12 
GeneralSlightly upgraded version of this Pinmemberkriskomar28-Apr-09 8:17 
GeneralException from HRESULT: 0x800A03EC while using SAVE AS with Save File Dialog Pinmemberkarthizen14-Mar-09 7:02 
GeneralThanks for posting this... Pinmemberstopgo6-Mar-09 22:29 
GeneralExcel 2007 Row Limit Problem Pinmemberreturnofjedi4-Mar-09 4:29 
GeneralRe: Excel 2007 Row Limit Problem - Solved Pinmemberreturnofjedi4-Mar-09 5:09 
GeneralI am getting this error each time i run the code PinmemberMohammad Al Hoss20-Feb-09 2:14 
GeneralRe: I am getting this error each time i run the code PinmemberPeterMoon20-Feb-09 15:40 
GeneralProblem solved PinmemberLuthandoman9-Feb-09 23:43 
GeneralRe: Problem solved PinmemberPeterMoon20-Feb-09 15:42 
QuestionThe type or namespace name 'ApplicationClass' could not be found Pinmemberravindra_satyam12-Dec-08 2:39 
AnswerRe: The type or namespace name 'ApplicationClass' could not be found PinmemberPeterMoon12-Dec-08 14:36 
GeneralBut what if the data is in DataSet format PinmemberAleksandra Czajka18-Jun-08 10:42 
GeneralRe: But what if the data is in DataSet format PinmemberCikaPero15-Jul-10 2:26 
GeneralValue instead of Value2 Pinmemberkneckedeck28-May-08 21:15 
GeneralRe: Value instead of Value2 PinmemberCodeProjectCraig19-Feb-10 1:14 

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
Web04 | 2.8.140926.1 | Last Updated 1 Dec 2007
Article Copyright 2007 by PeterMoon
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid