Epplus delete all rows from specific row

前端 未结 2 633
执念已碎
执念已碎 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:10

    Above solution is to delete last empty rows in the file. This will not work if file has empty rows in the middle of the rows list somewhere.

    Below is the solution to identify the empty rows in the middle of the rows list.

    I used combination of both above and mine to delete empty rows at the end of the rows list and empty rows in the middle of the rows list

     private void TrimEmptyRows(ExcelWorksheet worksheet)
        {
            //loop all rows in a file
            for (int i = worksheet.Dimension.Start.Row; i <= 
           worksheet.Dimension.End.Row; i++)
            {
                bool isRowEmpty = true;
                //loop all columns in a row
                for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
                {
                    if (worksheet.Cells[i, j].Value != null)
                    {
                        isRowEmpty = false;
                        break;
                    }
                }
                if (isRowEmpty)
                {
                    worksheet.DeleteRow(i);
                }
            }
        } 
    

提交回复
热议问题