how to get EPPlus OpenXML row count (c#)

前端 未结 5 1693
无人共我
无人共我 2021-01-17 09:49

I searched for it and found the link C# EPPlus OpenXML count rows

int iRowCount = currentWorksheet.Dimension.End.Row - currentWorksheet.Dimension.Start.Row;
         


        
5条回答
  •  失恋的感觉
    2021-01-17 10:01

    Empty cells in a worksheet may still contain formatting causing them to be counted in the sheet Dimension:

    Empty cells can be cleared using the steps here: http://office.microsoft.com/en-au/excel-help/locate-and-reset-the-last-cell-on-a-worksheet-HA010218871.aspx

    I wrote this function to get the last row that contains text:

    int GetLastUsedRow(ExcelWorksheet sheet) {
      if (sheet.Dimension == null) {  return 0; } // In case of a blank sheet
        var row = sheet.Dimension.End.Row;
        while(row >= 1) {
            var range = sheet.Cells[row, 1, row, sheet.Dimension.End.Column];
            if(range.Any(c => !string.IsNullOrEmpty(c.Text))) {
                break;
            }
            row--;
        }
        return row;
    }
    

提交回复
热议问题