I want to select a range from the first to the last cell filled in the row or column. In VBA the code stays as below using xlDown or xlToRight.
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
How could I do it the same way in C # using Epplus? I will start from cell B139 and I must go to the last row and column
An important thing to know about the Cells object in an Worksheet in EPPlus is that it contains only references to cell that have data added to it. So with a little bit of LINQ you can get the address of every "Row" like this:
var lastRowCell1 = worksheet.Cells.Last(c => c.Start.Row == 1);
var lastRowCell2 = worksheet.Cells.Last(c => c.Start.Row == 2);
var lastColCell1 = worksheet.Cells.Last(c => c.Start.Column == 1);
var lastColCell2 = worksheet.Cells.Last(c => c.Start.Column == 2);
To get the last Cell's index you can use worksheet.Dimension
:
int numCol = worksheet.Dimension.Rows;
int numCol = worksheet.Dimension.Columns;
If you want the last address of Column or Row you can use this:
String lastAddress = worksheet.Dimension.Address.Last().ToString();
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