Click here to Skip to main content
Licence 
First Posted 1 Jul 2006
Views 279,999
Downloads 5,252
Bookmarked 189 times

Fast Excel file reader with basic functionality

By | 1 Jul 2006 | Article
A set of managed classes for native reading of data from MS Excel files

Introduction

When I was writing one of the projects (multi-format data parser) I had to deal with Microsoft Excel files.

First of all I tried to read data via COM interop, but this way was very slow (and sometimes input files have up to 100,000 rows and even more) and required Excel to be installed on the target machine.

Then I tried to read data via Excel's OleDb driver. When DataReader ran through data rows without reading them (i.e. while (rd.Read());) it was quite fast, but retrieving data was still very slow.

When I had tried out one of commercial products (TMS FlexCel Studio), it showed rather good results (several seconds for loading 17 MB file, 2 sheets, ~ 80 000 rows), but access to them was not very fast (at least not suitable for displaying data using DataGridView in virtual mode).

So, I decided to write my own reader for Excel files.

Background

First of all I decided that the reader will produce a DataSet object, with sheets represented as DataTables within it. Access to DataTable is rather fast, and its TableName property is suitable for sheet title.

To read an Excel file, we need to get BIFF stream from xls OLE2 container. Working with OLE2 container is implemented in the following classes:

  • XlsHeader (file header representation)
  • XlsFat (support for file system in OLE2 container)
  • XlsRootDirectory (directory tree representation)
  • XlsDirectoryEntry, and
  • XlsStream (file stream)

Note that implementation of OLE2 container object model is limited (e.g. I didn't write any MiniFAT support code) and Excel-specific (XlsHeader supports only MS Excel header field values, any other will produce InvalidHeaderException).

Now we can parse BIFF stream with spreadsheet data. For handy reading data from stream we have a XlsBiffStream class derived from XlsStream. It provides a set of methods for reading XlsBiffRecord common structures. And then, each structure is converted to the appropriate type. Note that only a few record types are supported. All other structures are read as basic XlsBiffRecord and are ignored.

My implementation of reader ignores any text formatting, graphics, embedded objects and charts. Only cell values are read. When a cell contains a formula, the last calculated formula value is used as the cell value. Also, only indexed sheets are read (i.e. sheets with index data) - I didn't write not-indexed sheets support due to my laziness (I didn't come across any file without an index yet).

Using the code

Now, it's very easy to use the parser. All you need is a Stream with an Excel file.

FileStream fs = new FileStream(@"c:\file.xls", 
        FileMode.Open, FileAccess.Read);
ExcelDataReader rd = new ExcelDataReader(fs);
fs.Close();

// Now we can access data:
DataSet data = rd.WorkbookData;

Points Of Interest

Latest versions of Excel use SST (Shared String Table) to store string information, and text cells only reference string by index. Also, the size of one BIFF section is limited, and SST can be rather big, so it uses the so-called CONTINUE sections. Every string in SST can be saved as Unicode (2 byte) or as ANSI (single byte per symbol). But when the string is broken with the continue section, it can change its encoding from one to another! This took me a lot of debugging, and I wish nobody has to do the same again...

Results

This implementation of reader is rather nonoptimal (I was pressed for time), but it showed a good speed. It surely beats any interop or OLE method and even some commercial analogs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Messir

Web Developer

Russian Federation Russian Federation

Member

Alex
.NET Developer
Russian Federation
rakemaker@gmail.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLabels read wrong [modified] Pinmemberdmitry_andrianov7:54 13 Feb '12  
AnswerDateTime value are stored as floating point Pinmemberedvox113819:49 13 Oct '11  
QuestionOnly 16 columns can be imported PinmemberOsman Kazi11:38 30 Sep '11  
QuestionTrying to open an invalid file keeps the file open Pinmemberairadier2:58 23 Aug '11  
GeneralMy vote of 4 Pinmemberstuckne15:55 2 Feb '11  
GeneralRe: My vote of 4 PinmemberNguyen Thanh Hai6:20 26 Jul '11  
GeneralOops! Trying to read stream from FAT area. Pinmemberreferee220:00 13 Dec '10  
GeneralRe: Oops! Trying to read stream from FAT area. Pinmemberflash66522:36 5 Apr '11  
Generalseems does not support multiple worksheet Pinmemberbluesky44852:08 1 Nov '10  
GeneralDownload source Pinmembersutter1822:37 28 Sep '10  
GeneralRe: Download source Pinmemberblam202014:35 21 Jan '11  
GeneralThis library doesn't seem to include the top row as headers or support xlsx format Pinmemberakantro3:14 8 Aug '10  
GeneralRe: This library doesn't seem to include the top row as headers or support xlsx format Pinmemberkaferkopmetluise22:43 15 Apr '12  
GeneralFor excel 2007 & 2010 PinmemberMember 440278921:35 25 Jul '10  
GeneralHidden sheets Pinmemberreferee20:34 8 Jul '10  
QuestionUpdate Code PinmemberJoel Lovison12:25 13 Oct '09  
GeneralHyperlinks PinmembernKognito21:39 6 Oct '09  
GeneralError executing library in MONO Pinmembereyanson12:43 26 Sep '09  
GeneralRe: Error executing library in MONO Pinmembergg42374:16 14 Oct '09  
Generalplease explain the code to me please its urgent............. Pinmemberprasanth.shenoy18:12 25 Jun '09  
GeneralFailed to read column 16 Pinmemberalper ebicoglu22:34 11 Jun '09  
GeneralRe: Failed to read column 16 PinmemberRichard at BDG4:30 20 Aug '09  
QuestionReading a protected workbook PinmemberHervé12:41 29 Mar '09  
QuestionDate Is not Code Pinmemberjatin_chauhan0:08 26 Mar '09  
GeneralRe: Date Is not Code PinmemberGirijesh15:12 16 Apr '09  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 1 Jul 2006
Article Copyright 2006 by Messir
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid