Nate Pet
Nate Pet

Reputation: 46222

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.

Upvotes: 57

Views: 158216

Answers (4)

RBT
RBT

Reputation: 25877

I'm not sure if this solution will solve OP's problem but I prefer using RowsUsed method. It can be used to get the list of only those rows which are non-empty or has been edited by the user. This way I can avoid making emptiness check while processing each row.

Below code snippet can process 3rd to 20th row numbers out of all the non-empty rows. I've filtered the empty rows before starting the foreach loop. Please bear in mind that filtering the non-empty rows before starting to process the rows can affect the total count of rows which will get processed. So you need to be careful while applying any logic which is based on the total number of rows processed inside foreach loop.

string fileName = "C:\\Folder1\\Prev.xlsx";
using (var excelWorkbook = new XLWorkbook(fileName))
{
    var nonEmptyDataRows = excelWorkbook.Worksheet(1).RowsUsed();

    foreach (var dataRow in nonEmptyDataRows)
    {
       //for row number check
       if(dataRow.RowNumber() >=3 && dataRow.RowNumber() <= 20)
       {
           //to get column # 3's data
           var cell = dataRow.Cell(3).Value;
       }
    }
}

RowsUsed method is helpful in commonly faced problems which require processing the rows of an excel sheet.

Upvotes: 26

Saeid
Saeid

Reputation: 623

It works easily

 XLWorkbook workbook = new XLWorkbook(FilePath);
 var rowCount = workbook.Worksheet(1).LastRowUsed().RowNumber();
 var columnCount = workbook.Worksheet(1).LastColumnUsed().ColumnNumber();
 int column = 1;
 int row = 1;
 List<string> ll = new List<string>();
 while (row <= rowCount)
 {
      while (column <= columnCount)
      {
         string title = workbook.Worksheets.Worksheet(1).Cell(row, column).GetString();
                ll.Add(title);
                column++;
       }

 row++;
 column = 1;
}

Upvotes: 2

Jess
Jess

Reputation: 25039

Here's my jam.

var rows = worksheet.RangeUsed().RowsUsed().Skip(1); // Skip header row
foreach (var row in rows)
{
    var rowNumber = row.RowNumber();
    // Process the row
}

If you just use .RowsUsed(), your range will contain a huge number of columns. Way more than are actually filled in!

So use .RangeUsed() first to limit the range. This will help you process the file faster!

You can also use .Skip(1) to skip over the column header row (if you have one).

Upvotes: 32

Raidri
Raidri

Reputation: 17550

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.

Upvotes: 60

Related Questions