Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from Excel File using ClosedXML

My Excel file is not in tabular data. I am trying to read from an excel file. I have sections within my excel file that are tabular.

I need to loop through rows 3 to 20 which are tabular and read the data.

Here is party of my code:

     string fileName = "C:\\Folder1\\Prev.xlsx";      var workbook = new XLWorkbook(fileName);      var ws1 = workbook.Worksheet(1);  

How do I loop through rows 3 to 20 and read columns 3,4, 6, 7, 8? Also if a row is empty, how do I determine that so I can skip over it without reading that each column has a value for a given row.

like image 828
Nate Pet Avatar asked Apr 15 '15 16:04

Nate Pet


People also ask

How do I read an Excel file in ClosedXML?

C# read Excel file using ClosedXML. Excel; using var wbook = new XLWorkbook("simple. xlsx"); var ws1 = wbook. Worksheet(1); var data = ws1.

Does ClosedXML require Excel?

ClosedXML allows you to create Excel files without the Excel application. The typical example is creating Excel reports on a web server.

What is the difference between OpenXml and ClosedXML?

Macros – ClosedXml doesn't support macros as its base library OpenXml also doesn't support it. Embedding – We cannot embed any file into Excel using ClosedXml, no APIs built for that, so some features of OpenXml still need to be implemented. Charts – No functionality related to charting is present.


1 Answers

To access a row:

var row = ws1.Row(3); 

To check if the row is empty:

bool empty = row.IsEmpty(); 

To access a cell (column) in a row:

var cell = row.Cell(3); 

To get the value from a cell:

object value = cell.Value; // or string value = cell.GetValue<string>(); 

For more information see the documentation.

like image 173
Raidri Avatar answered Sep 26 '22 14:09

Raidri