Click here to Skip to main content
15,439,290 members
Articles / Programming Languages / C#
Posted 1 Jul 2006


211 bookmarked

Fast Excel file reader with basic functionality

Rate me:
Please Sign up or sign in to vote.
4.88/5 (104 votes)
1 Jul 20063 min read
A set of managed classes for native reading of data from MS Excel files


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.


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);

// 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...


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.


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

Written By
Web Developer
Russian Federation Russian Federation
.NET Developer
Russian Federation

Comments and Discussions

Generalgreat code but not processing booleans Pin
Alpha_100036-Sep-06 22:50
MemberAlpha_100036-Sep-06 22:50 
GeneralERROR: Oops! There's no such sector in FAT. Pin
jadvnstamford5-Sep-06 11:07
Memberjadvnstamford5-Sep-06 11:07 
GeneralRe: ERROR: Oops! There's no such sector in FAT. Pin
Messir5-Sep-06 11:38
MemberMessir5-Sep-06 11:38 
GeneralRe: ERROR: Oops! There's no such sector in FAT. Pin
Kelevr43-Nov-08 5:16
MemberKelevr43-Nov-08 5:16 
QuestionDimensions Pin
zericsson5-Sep-06 4:48
Memberzericsson5-Sep-06 4:48 
AnswerRe: Dimensions Pin
Messir5-Sep-06 9:37
MemberMessir5-Sep-06 9:37 
GeneralRe: Dimensions Pin
zericsson5-Sep-06 20:33
Memberzericsson5-Sep-06 20:33 
QuestionDeclimals Pin
jadvnstamford30-Aug-06 13:06
Memberjadvnstamford30-Aug-06 13:06 
The code works great... I have a spreadsheet with values below the second decimal place (0.005) but it returns these values as 0. Any idea why and what I need to do?

AnswerRe: Declimals Pin
Messir30-Aug-06 14:09
MemberMessir30-Aug-06 14:09 
QuestionHow to get the file path from hyperlink cell Pin
chandrut19-Aug-06 22:05
Memberchandrut19-Aug-06 22:05 
AnswerRe: How to get the file path from hyperlink cell Pin
Messir20-Aug-06 9:37
MemberMessir20-Aug-06 9:37 
QuestionParsing of dates Pin
levelnis14-Aug-06 5:50
Memberlevelnis14-Aug-06 5:50 
AnswerRe: Parsing of dates Pin
levelnis14-Aug-06 6:03
Memberlevelnis14-Aug-06 6:03 
GeneralRe: Parsing of dates Pin
dodiggitydag15-Aug-06 7:44
Memberdodiggitydag15-Aug-06 7:44 
AnswerRe: Parsing of dates Pin
kaitv20-Mar-07 2:02
Memberkaitv20-Mar-07 2:02 
AnswerRe: Parsing of dates Pin
levelnis20-Mar-07 2:09
Memberlevelnis20-Mar-07 2:09 
GeneralRe: Parsing of dates Pin
kaitv20-Mar-07 2:59
Memberkaitv20-Mar-07 2:59 
GeneralRe: Parsing of dates Pin
dvhemert18-Apr-08 4:35
Memberdvhemert18-Apr-08 4:35 
GeneralExcellent Pin
Muhammad Adeel Malik13-Aug-06 4:52
MemberMuhammad Adeel Malik13-Aug-06 4:52 
GeneralHyperlink Pin
Devendra Jindal11-Aug-06 2:18
MemberDevendra Jindal11-Aug-06 2:18 
GeneralFull Source Code Pin
Deepankar8-Aug-06 3:59
MemberDeepankar8-Aug-06 3:59 
GeneralRe: Full Source Code Pin
Messir8-Aug-06 6:03
MemberMessir8-Aug-06 6:03 
GeneralRe: Full Source Code Pin
Deepankar8-Aug-06 8:06
MemberDeepankar8-Aug-06 8:06 
GeneralRe: Full Source Code Pin
Messir10-Aug-06 5:33
MemberMessir10-Aug-06 5:33 
QuestionRe: Full Source Code Pin
Deepankar10-Aug-06 7:00
MemberDeepankar10-Aug-06 7:00 

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.