Click here to Skip to main content
6,632,966 members and growing! (20,025 online)
Email Password   helpLost your password?
Enterprise Systems » Office Development » Microsoft Excel     Intermediate License: The GNU Lesser General Public License

Excel Reader

By Liu Junfeng

Create, read and modify Excel *.xls files in pure C# without COM interop
C# 2.0, Windows, .NET 2.0, Visual Studio, Dev
Version:7 (See All)
Posted:1 Nov 2006
Updated:20 Feb 2009
Views:167,480
Bookmarked:205 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
58 votes for this article.
Popularity: 8.18 Rating: 4.64 out of 5
1 vote, 1.8%
1
2 votes, 3.5%
2

3
3 votes, 5.3%
4
51 votes, 89.5%
5
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

About the Author

Liu Junfeng


Member

Occupation: Software Developer
Location: China China

Other popular Office Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 124 (Total in Forum: 124) (Refresh)FirstPrevNext
GeneralError: An item with the same key has already been added Pinmemberisragc6:52 22 Sep '09  
GeneralInserting columns PinmemberScotty864:14 25 Aug '09  
GeneralRe: Inserting columns PinmemberScotty864:58 25 Aug '09  
QuestionLost formulas PinmemberAle_Monta8:11 6 Aug '09  
AnswerRe: Lost formulas PinmemberLiu Junfeng17:45 10 Aug '09  
GeneralHyperlinks to relative paths Pinmembermickymoo17:13 14 Jul '09  
GeneralAlternative PinmemberFilipKrnjic0:04 8 Jul '09  
GeneralRe: Alternative Pinmemberspencepk9:02 21 Sep '09  
Generalcell markup Pinmemberkoosvanderwerf22:47 14 Jun '09  
GeneralRe: cell markup Pinmemberjkuhlz6:52 18 Jun '09  
GeneralOut Of Memory Error Pinmembercblaze2221:06 19 May '09  
GeneralAfter writing and then read the Excel document error endofstreamException was unhandeld PinmemberDekkie0:38 21 Apr '09  
QuestionEncode directly for webpage Response? PinmemberHenrik Uhlen21:10 19 Apr '09  
AnswerRe: Encode directly for webpage Response? PinmemberLiu Junfeng21:23 23 Apr '09  
QuestionRe: Encode directly for webpage Response? Pinmembermaxnm4:39 19 Jun '09  
AnswerRe: Encode directly for webpage Response? Pinmembermaxnm0:31 21 Jun '09  
AnswerRe: Encode directly for webpage Response? PinmemberLiu Junfeng18:44 2 Jul '09  
GeneralHow to know the last row number ? PinmemberExceter3:31 16 Apr '09  
AnswerRe: How to know the last row number ? PinmemberDekkie5:34 20 Apr '09  
GeneralRe: How to know the last row number ? PinmemberLiu Junfeng0:40 21 Apr '09  
GeneralRe: How to know the last row number ? PinmemberExceter21:47 22 Apr '09  
QuestionDatetime problem Pinmemberbenbell16:44 16 Mar '09  
AnswerRe: Datetime problem PinmemberLiu Junfeng18:28 16 Mar '09  
GeneralExcel 2007 Format PinmemberBethH14:27 5 Mar '09  
QuestionCell Background color PinmemberMember 33296500:07 2 Mar '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Feb 2009
Editor: Deeksha Shenoy
Copyright 2006 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project