I'm using ClosedXML with C# to modify an Excel workbook. I need to find the last row number used but .RowCount() counts how many rows are in the worksheet. So it is returning 1 million rows when there are only a few thousand. I have tried LastRowUsed() but that doesn't return an int and there is no .row method for it. How can I get the last row in an int?
9
votes
Daniel, I think you marked the wrong comment as the answer.
- Francois Botha
4 Answers
21
votes
9
votes
4
votes
You have to use .LastRowUsed()
More useful stuff from official GitHub repo:
range.FirstCell()
range.FirstCellUsed()
range.FirstColumn()
range.FirstColumnUsed()
range.FirstRow()
range.FirstRowUsed()
range.LastCell()
range.LastCellUsed()
range.LastColumn()
range.LastColumnUsed()
range.LastRow()
range.LastRowUsed()
0
votes
I used this code
libro = new XLWorkbook("file.xlsx");
hoja = libro.Worksheet(1);
var primeraCeldaUsada = hoja.FirstCellUsed();
var ultimaCeldaUsada = hoja.LastCellUsed();
var rango = hoja.Range(primeraCeldaUsada.Address, ultimaCeldaUsada.Address);
int ultfila = int.Parse(ultimaCeldaUsada.Address.RowNumber.ToString());
for (int i = 7; i <= ultfila; i++)
{
hoja.Cell(i, 3).Value = hoja.Cell(i, 3).Address.ToString();
}