Click here to Skip to main content
15,886,030 members
Articles / Programming Languages / C#

Excel Reader

Rate me:
Please Sign up or sign in to vote.
4.92/5 (109 votes)
20 Feb 2009LGPL32 min read 1.1M   25.8K   357   173
Create, read and modify Excel *.xls files in pure C# without COM interop
Sample Image - ExcelReader.png

Introduction

This library is built based on the following documents, thanks to their authors:

Record structures in BIFF8/BIFF8X format are considered.

Now this project is hosted on Google Code.
You can get the latest code, report issues and submit improvements from there. 

What It Can Do?

  • It can read worksheets in a workbook and read cells in a worksheet.
  • It can read cell content (text, number, datetime, or error) and cell format (font, alignment, linestyle, background, etc.).
  • It can read pictures in the file, get information of image size, position, data, and format.
  • It can create a workbook and save to file.

Using the Code

  1. Open file:
    C#
    Stream fileStream = File.OpenRead(file);
    Workbook book = new Workbook();
    book.Open(fileStream);
    Worksheet sheet = book.Worksheets[0];
  2. Read cell:
    C#
    int row = 1;
    int col = 0;
    string ID = sheet.Cells[row, col].StringValue;
    
    Picture pic = sheet.ExtractPicture(row, col);
  3. Create workbook:
    C#
    string file = "C:\\newdoc.xls";
    Workbook workbook = new Workbook();
    Worksheet worksheet = new Worksheet("First Sheet");
    worksheet.Cells[0, 1] = new Cell(1);
    worksheet.Cells[2, 0] = new Cell(2.8);
    worksheet.Cells[3, 3] = new Cell((decimal)3.45);
    worksheet.Cells[2, 2] = new Cell("Text string");
    worksheet.Cells[2, 4] = new Cell("Second string");
    worksheet.Cells[4, 0] = new Cell(32764.5, "#,###.00");
    worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
    worksheet.Cells.ColumnWidth[0, 1] = 3000;
    workbook.Worksheets.Add(worksheet);
    workbook.Save(file);
  4. Traverse worksheet:
    C#
    // traverse cells
    foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
    {
        dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
    }
    
    // traverse rows by Index
    for (int rowIndex = sheet.Cells.FirstRowIndex;
       rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
    {
        Row row = sheet.Cells.GetRow(rowIndex);
        for (int colIndex = row.FirstColIndex;
       colIndex <= row.LastColIndex; colIndex++)
        {
            Cell cell = row.GetCell(colIndex);
        }
    }
    

History

  • 2007-5-17
    • Displayed each Sheet in separate tabpage
    • Fixed some bugs in FORMULA.cs and Form1.cs
  • 2007-5-26
    • Decoded FORMULAR result
  • 2008-1-23
    • Changed default Encoding from ASCII to UTF8 in Record.cs ln97 (suggested by ragundo)
    • Return EmptyCell instead of null for non existing cells (requested by amrlafi)
    • Bug fix in Record.cs ln83 and ln133 (proposed by dhirshjr and ilogpasig)
    • Tried to fix bug in CompoundDocument.cs (found by stevenbright)
  • 2008-9-15
    • Create, Open and Modify CompoundDocument
  • 2008-9-22
    • Save modified CompoundDocument
  • 2008-10-26
    • Encoded workbook and worksheet to BIFF8 records
    • Fixed a bug that compressed Unicode string wrongly decoded by UTF8
  • 2008-11-10
    • Fixed XF format so that the created *.xls file can be opened by Microsoft Excel 2003
  • 2008-11-16
    • Assigned number format for number and date time values
    • Set column widths
  • 2009-1-3
    • Updated project source code and article text
    • Many bug fixes
    • Added support to create and modify *.xls files
  • 2009-01-20
    • Fix for getting 'NaN' when reading string value of a formula cell
    • Start a Google Code project
  • 2009-01-23
    • Support for decode cell format and encode predefined cell format
    • Changed namespaces and directory structure
    • Added some test cases
  • 2009-02-12
    • Initial implementation of encode images

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Pls Help Pin
Liu Junfeng16-Nov-07 2:42
Liu Junfeng16-Nov-07 2:42 
QuestionWrong DateTime with certain cell values? Pin
otyrland31-Oct-07 11:34
otyrland31-Oct-07 11:34 
GeneralProblem with PeekChar() Pin
ilogpasig25-Sep-07 15:54
ilogpasig25-Sep-07 15:54 
GeneralCoding errors, style comments. Pin
ThePhantasm8-Sep-07 7:51
ThePhantasm8-Sep-07 7:51 
GeneralRe: Coding errors, style comments. Pin
Liu Junfeng23-Sep-07 21:57
Liu Junfeng23-Sep-07 21:57 
GeneralRe: Coding errors, style comments. Pin
ilogpasig24-Sep-07 17:52
ilogpasig24-Sep-07 17:52 
GeneralRe: Coding errors, style comments. Pin
Dave Downs16-Oct-07 1:02
Dave Downs16-Oct-07 1:02 
GeneralRe: Coding errors, style comments. Pin
Marco Ensing15-Nov-07 5:17
Marco Ensing15-Nov-07 5:17 
Questiongenerating empty cells rather than substituting to avoid null exceptions Pin
amrlafi7-Sep-07 18:10
amrlafi7-Sep-07 18:10 
GeneralVery nice! Pin
B.V.Papadopoulos6-Sep-07 23:45
professionalB.V.Papadopoulos6-Sep-07 23:45 
GeneralA bug fix Pin
dhirshjr3-Aug-07 10:18
dhirshjr3-Aug-07 10:18 
GeneralRe: A bug fix Pin
Liu Junfeng10-Aug-07 0:58
Liu Junfeng10-Aug-07 0:58 
GeneralProblem with reading Excel which has a large number of columns Pin
Wen200227-Jul-07 5:47
Wen200227-Jul-07 5:47 
GeneralRe: Problem with reading Excel which has a large number of columns Pin
stevenbright14-Jan-08 10:52
stevenbright14-Jan-08 10:52 
GeneralProblem with array formulas Pin
madjab26-Jul-07 7:04
madjab26-Jul-07 7:04 
GeneralDate Format Pin
sameerhanda27-Jun-07 3:55
sameerhanda27-Jun-07 3:55 
AnswerRe: Date Format Pin
miropetkov4-Jul-07 6:07
miropetkov4-Jul-07 6:07 
GeneralRe: Date Format Pin
sameerhanda5-Jul-07 4:20
sameerhanda5-Jul-07 4:20 
GeneralRe: Date Format Pin
TuftedCat5-Jan-10 10:32
TuftedCat5-Jan-10 10:32 
GeneralFor manage accents Pin
Funeral11-Jun-07 2:45
Funeral11-Jun-07 2:45 
NewsUpdate notes Pin
Liu Junfeng29-May-07 3:35
Liu Junfeng29-May-07 3:35 
GeneralGood Effort Pin
Moim Hossain27-May-07 19:02
Moim Hossain27-May-07 19:02 
GeneralRe: Good Effort Pin
Liu Junfeng27-May-07 21:18
Liu Junfeng27-May-07 21:18 
GeneralHelp Pin
Jeff Circeo7-Mar-07 4:47
Jeff Circeo7-Mar-07 4:47 
GeneralRe: Help Pin
TomatGurka16-Mar-07 23:13
TomatGurka16-Mar-07 23:13 

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.