Click here to Skip to main content
11,580,000 members (76,306 online)
Click here to Skip to main content

Excel Reader

, 20 Feb 2009 LGPL3 569K 19.6K 338
Rate this:
Please Sign up or sign in to vote.
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:
    Stream fileStream = File.OpenRead(file);
    Workbook book = new Workbook();
    book.Open(fileStream);
    Worksheet sheet = book.Worksheets[0];
  2. Read cell:
    int row = 1;
    int col = 0;
    string ID = sheet.Cells[row, col].StringValue;
    
    Picture pic = sheet.ExtractPicture(row, col);
  3. Create workbook:
    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:
     // 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)

Share

About the Author

Liu Junfeng
Software Developer (Senior) Beyondsoft SH
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionexcel文件转成pdf Convert excel to pdf Pin
ming_775519-Mar-15 16:34
memberming_775519-Mar-15 16:34 
QuestionTks for thread! Pin
vietnam-travel-vietnam-tour25-Mar-14 20:40
professionalvietnam-travel-vietnam-tour25-Mar-14 20:40 
NewsMessage Automatically Removed Pin
16-Oct-13 22:51
memberJamie Calops16-Oct-13 22:51 
Questionformatting Pin
mbowles20129-Jan-13 5:41
membermbowles20129-Jan-13 5:41 
All formatting of original file is lost on save. Anyone found a solution to this? Makes it unusable for editing exisitng files.
-MickeyB

QuestionData amount related problem Pin
Llarian3-Sep-12 3:45
memberLlarian3-Sep-12 3:45 
GeneralMy vote of 5 Pin
tomercagan26-Aug-12 1:03
membertomercagan26-Aug-12 1:03 
QuestionHow can i dump a worksheet to a DataTable? Pin
navakiran19-Aug-12 1:18
membernavakiran19-Aug-12 1:18 
GeneralMy vote of 5 Pin
cdwilliams11-Jul-12 9:24
membercdwilliams11-Jul-12 9:24 
SuggestionC# Read Excel and Show in WPF DataGrid Pin
Member 471378514-Jun-12 4:49
memberMember 471378514-Jun-12 4:49 
QuestionError writing to excel when using japanese characters Pin
princektd13-Jun-12 20:29
memberprincektd13-Jun-12 20:29 
QuestionFix for Reading Excel with Macro Issue? Pin
gsanchezbiz27-Mar-12 21:12
membergsanchezbiz27-Mar-12 21:12 
QuestionImages count Pin
afxii15-Jan-12 2:37
memberafxii15-Jan-12 2:37 
QuestionCol count? Pin
Okan Kocyigit22-Dec-11 2:11
memberOkan Kocyigit22-Dec-11 2:11 
QuestionCharting/graphs Pin
forge33_se31-Aug-11 15:11
memberforge33_se31-Aug-11 15:11 
QuestionMerge cells Pin
plextoR24-Jul-11 0:44
memberplextoR24-Jul-11 0:44 
GeneralMy vote of 3 Pin
Reelix13-Apr-11 0:55
memberReelix13-Apr-11 0:55 
GeneralMy vote of 5 Pin
rajesh_chan28-Mar-11 22:36
memberrajesh_chan28-Mar-11 22:36 
GeneralClass reference Pin
senguptaamlan8-Mar-11 1:53
membersenguptaamlan8-Mar-11 1:53 
AnswerRe: Class reference Pin
DrkSpyder26-Dec-11 20:50
memberDrkSpyder26-Dec-11 20:50 
GeneralRe: Class reference Pin
Nick Merchant1-May-12 10:43
memberNick Merchant1-May-12 10:43 
GeneralMy vote of 5 Pin
careced1-Mar-11 7:36
membercareced1-Mar-11 7:36 
GeneralMy vote of 5 Pin
sneff28-Feb-11 2:51
membersneff28-Feb-11 2:51 
GeneralCompoundDocument should implement IDisopsable Pin
sneff28-Feb-11 2:30
membersneff28-Feb-11 2:30 
GeneralRe: CompoundDocument should implement IDisopsable Pin
Liu Junfeng31-Mar-11 6:07
memberLiu Junfeng31-Mar-11 6:07 
GeneralRe: CompoundDocument should implement IDisopsable Pin
sneff31-Mar-11 10:20
membersneff31-Mar-11 10:20 
GeneralMy vote of 5 Pin
ahsan sarfraz3-Feb-11 1:07
memberahsan sarfraz3-Feb-11 1:07 
GeneralMy vote of 5 Pin
Member 12187722-Dec-10 9:00
memberMember 12187722-Dec-10 9:00 
QuestionCan I read just an specific worksheet? Pin
javierv2-Dec-10 6:50
memberjavierv2-Dec-10 6:50 
GeneralMy vote of 1 Pin
Nilesh Kabra17-Nov-10 19:03
memberNilesh Kabra17-Nov-10 19:03 
QuestionHow To Get ForeColor of a Cell? Pin
Majid_grok7-Aug-10 2:32
memberMajid_grok7-Aug-10 2:32 
GeneralBest tool for reading and writing excel using c# Pin
harry peter8625-Jul-10 4:43
memberharry peter8625-Jul-10 4:43 
GeneralMy vote of 5 Pin
lionzl6-Jul-10 16:20
memberlionzl6-Jul-10 16:20 
GeneralWrite a file to a byte array instead of a physical file Pin
silvaan17-May-10 23:16
membersilvaan17-May-10 23:16 
General2010 Excel Issue while opening Pin
rleoanrd27-Apr-10 13:37
memberrleoanrd27-Apr-10 13:37 
GeneralRe: 2010 Excel Issue while opening Pin
sanme9825-Jul-10 17:39
membersanme9825-Jul-10 17:39 
GeneralRe: 2010 Excel Issue while opening Pin
Toivo_16-Nov-10 10:57
memberToivo_16-Nov-10 10:57 
GeneralRe: 2010 Excel Issue while opening Pin
Member 78731032-Aug-11 22:54
memberMember 78731032-Aug-11 22:54 
GeneralMicrosoft Office Excel did not find anything to print Pin
edgar_v_reyes17-Dec-09 12:22
memberedgar_v_reyes17-Dec-09 12:22 
GeneralMerging Pin
Jon Law5-Dec-09 9:17
memberJon Law5-Dec-09 9:17 
GeneralError: An item with the same key has already been added Pin
isragc22-Sep-09 5:52
memberisragc22-Sep-09 5:52 
GeneralInserting columns Pin
Scotty8625-Aug-09 3:14
memberScotty8625-Aug-09 3:14 
GeneralRe: Inserting columns Pin
Scotty8625-Aug-09 3:58
memberScotty8625-Aug-09 3:58 
QuestionLost formulas Pin
Ale_Monta6-Aug-09 7:11
memberAle_Monta6-Aug-09 7:11 
AnswerRe: Lost formulas Pin
Liu Junfeng10-Aug-09 16:45
memberLiu Junfeng10-Aug-09 16:45 
GeneralHyperlinks to relative paths Pin
mickymoo14-Jul-09 16:13
membermickymoo14-Jul-09 16:13 
GeneralAlternative Pin
FilipKrnjic7-Jul-09 23:04
memberFilipKrnjic7-Jul-09 23:04 
GeneralRe: Alternative Pin
spencepk21-Sep-09 8:02
memberspencepk21-Sep-09 8:02 
Generalcell markup Pin
koosvanderwerf14-Jun-09 21:47
memberkoosvanderwerf14-Jun-09 21:47 
GeneralRe: cell markup Pin
jkuhlz18-Jun-09 5:52
memberjkuhlz18-Jun-09 5:52 
GeneralRe: cell markup Pin
SH_223-Oct-11 10:16
memberSH_223-Oct-11 10:16 

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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 20 Feb 2009
Article Copyright 2006 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid