Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last cell in row and column in Epplus - C#

Tags:

c#

epplus

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

like image 457
G. Sena Avatar asked Mar 02 '17 14:03

G. Sena


2 Answers

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);
like image 154
Gabriel Bernardone Avatar answered Sep 27 '22 15:09

Gabriel Bernardone


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();
like image 36
daniele3004 Avatar answered Sep 27 '22 17:09

daniele3004