How can I programtically find out the last non-empty row of a particular column (say column 1) using EPPlus & c#?
For testing I've create a test.xlsx file (shown below)
| ID | Name | Address | Status |
| 002 | John | USA | |
So, in this case the last non-empty row of column 1 should be 2. I've tried the below code but it shows me 10 instead of 2 (I think since I've formatted the cells till cell D10 like added some border?)
var myFile = new ExcelPackage(new FileInfo(@"test.xlsx"));
Excelsheet sheet = myFile.Workbook.sheets[0];
MessageBox.Show(sheet.Dimension.End.Row.ToString());
So I tried
var lastRowOfCol1 = sheet.Cells.Where(c=>c.Start.Column == 1 && c.End.Column == 1).GroupBy(x=>x.Value.ToString() !="").Select(p=>p.Last());
MessageBox.Show(lastRowOfCol1.ToString());
Now I'm getting

Can anyone help me on this?
Satya KarkiPosted Dec 19, 2021, 6:41 AM
You can try the below sample code and modify it according to your excel data.
Leon DPosted Dec 20, 2021, 10:14 AM
Yazhini MPPosted Dec 20, 2021, 5:40 AM
This is the sample example,
You can set a bool in the for loop at row level. Then loop all the cells and change the bool when a cell is not empty.
//loop through rows and columns
for (int row = 1; row <= rowCount; row++)
{
//create a bool
bool RowIsEmpty = true;
for ( int col =1; col <= colCount; col++)
{
//check if the cell is empty or not
if (worksheet.Cells[row, col].Value != null)
{
RowIsEmpty = false;
}
}
//dispaly result
if (RowIsEmpty)
{
Label1.Text += "Row " + row + " is empty.
";
}
}