Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file using NPOI

Tags:

c#

.net

excel

npoi

I found NPOI is very good to write Excel files with C#.

But I want to open, read and modify Excel files in C#.

How can I do this?

like image 631
Akhil Avatar asked May 02 '11 10:05

Akhil


People also ask

Is Npoi free?

Quite a few libraries are available, but there's one that shines: NPOI. It's a C# port of the POI Java project by Apache, and contrary to some of its competitors, it's free, it's open source, and it's a stand-alone implementation, that is, no interop.

What is Npoi?

NPOI is an open source project which can help you read/write XLS, DOC, PPT file extensions. This tool is the .NET version of POI Java project (http://poi.apache.org/). It covers most of the features of Excel like styling, formatting, data formulas, extract images, etc.

What is difference between XSSF and HSSF?

HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.

What is XSSFWorkbook in Java?

XSSFWorkbook. It is a class that is used to represent both high and low level Excel file formats. It belongs to the org. apache. xssf.


1 Answers

Simple read example below:

using NPOI.HSSF.UserModel; using NPOI.SS.UserModel;  //.....  private void button1_Click(object sender, EventArgs e) {     HSSFWorkbook hssfwb;     using (FileStream file = new FileStream(@"c:\test.xls", FileMode.Open, FileAccess.Read))     {         hssfwb= new HSSFWorkbook(file);     }      ISheet sheet = hssfwb.GetSheet("Arkusz1");     for (int row = 0; row <= sheet.LastRowNum; row++)     {         if (sheet.GetRow(row) != null) //null is when the row only contains empty cells          {             MessageBox.Show(string.Format("Row {0} = {1}", row, sheet.GetRow(row).GetCell(0).StringCellValue));         }     } }   

By the way: on NPOI website here in Download section there is example package - a pack of C# examples. Try it, if you haven't yet. :)

like image 108
mj82 Avatar answered Sep 18 '22 21:09

mj82