问题
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
回答1:
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);
回答2:
To get the last Ceels index you che get the value by thi way:
int numCol = worksheet.Dimension.Rows;
int numCol = worksheet.Dimension.Columns;
If you want the last address of Column or Row you cn ue this:
String lastAddress = worksheet.Dimension.Address.Last().ToString();
来源:https://stackoverflow.com/questions/42558303/last-cell-in-row-and-column-in-epplus-c-sharp