Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate through rows in an excel table using epplus?

Tags:

c#

excel

epplus

I am new to epplus, and i'm trying to read some values from an excel table.

This is what I have so far:

var fileInfo = new FileInfo(filename); using(var excelPackage = new OfficeOpenXml.ExcelPackage(fileInfo)) {     foreach (var sheet in excelPackage.Workbook.Worksheets)     {         foreach (ExcelTable table in sheet.Tables)         {              foreach(var row in table.Rows)  // <-- !!              { ... }         }     } } 

However, now I am stumped, as the ExcelTable only has a Columns property, but not a Rows property as I had expected. I cannot find a Rows property on any object in the library.

How do I iterate through a table, reading Row for Row?

like image 617
oɔɯǝɹ Avatar asked Feb 12 '14 23:02

oɔɯǝɹ


People also ask

What is the use of EPPlus?

EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.

How do I merge cells in EPPlus?

If you want to merge cells dynamically, you can also use: worksheet. Cells[FromRow, FromColumn, ToRow, ToColumn].

Can EPPlus read XLS?

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.


1 Answers

While searching for help on the same problem, I stumbled across this link. It certainly worked for me! Definitely better than using Interop objects. :)

I adapted it slightly though:

var package = new ExcelPackage(new FileInfo("sample.xlsx"));  ExcelWorksheet workSheet = package.Workbook.Worksheets[0]; var start = workSheet.Dimension.Start; var end = workSheet.Dimension.End; for (int row = start.Row; row <= end.Row; row++) { // Row by row...     for (int col = start.Column; col <= end.Column; col++)     { // ... Cell by cell...         object cellValue = workSheet.Cells[row, col].Text; // This got me the actual value I needed.     } } 
like image 80
Chris Paton Avatar answered Sep 21 '22 08:09

Chris Paton