Epplus delete all rows from specific row

前端 未结 2 647
执念已碎
执念已碎 2021-01-03 00:52

It is possible to somehow delete all following rows from specific (empty) row ? I tried for cyclus

            for (int rowNum = 1; rowNum <= worksheet.D         


        
2条回答
  •  独厮守ぢ
    2021-01-03 01:19

    I know that it is old but I could not find any solution so made one my by own. It is checking the last row if it is empty and if yes it deletes it and doing this until finds non-empty row. (non-empty means here: all columns in this row have some value)

    worksheet.TrimLastEmptyRows();
    
    public static void TrimLastEmptyRows(this ExcelWorksheet worksheet)
        {
            while (worksheet.IsLastRowEmpty())
                worksheet.DeleteRow(worksheet.Dimension.End.Row);
        }
    
    public static bool IsLastRowEmpty(this ExcelWorksheet worksheet)
        {
            var empties = new List();
    
            for (int i = 1; i <= worksheet.Dimension.End.Column; i++)
            {
                var rowEmpty = worksheet.Cells[worksheet.Dimension.End.Row, i].Value == null ? true : false;
                empties.Add(rowEmpty);
            }
    
            return empties.All(e => e);
        }
    

提交回复
热议问题