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?
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.
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.
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.
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.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With