NPOI - Get excel row count to check if it is empty

谁说胖子不能爱 提交于 2019-12-23 09:31:49

问题


I'm reading an xlsx file using NPOI lib, with C#. I need to extract some of the excel columns and save the extracted values into some kind of data structure.

I can successfully read the file and get all the values from the 2nd (the first one contains only headers) to the last row with the following code:

...
workbook = new XSSFWorkbook(fs);
sheet = (XSSFSheet)workbook.GetSheetAt(0);
....
int rowIndex = 1;  //--- SKIP FIRST ROW (index == 0) AS IT CONTAINS TEXT HEADERS
while (sheet.GetRow(rowIndex) != null) {
    for (int i = 0; i < this.columns.Count; i++){
       int colIndex = this.columns[i].colIndex;
       ICell cell = sheet.GetRow(rowIndex).GetCell(colIndex);
       cell.SetCellType(CellType.String);
       String cellValue = cell.StringCellValue;
       this.columns[i].values.Add(cellValue); //--- Here I'm adding the value to a custom data structure
    }
    rowIndex++;
}

What I'd like to do now is check if the excel file is empty or if it has only 1 row in order to properly handle the issue and display a message

If I run my code against an excel file with only 1 row (headers), it breaks on

cell.SetCellType(CellType.String); //--- here cell is null

with the following error:

Object reference not set to an instance of an object.

I also tried to get the row count with

sheet.LastRowNum

but it does not return the right number of rows. For example, I have created an excel with 5 rows (1xHEADER + 4xDATA), the code reads successfully the excel values. On the same excel I have removed the 4 data rows and then I have launched again the code on the excel file. sheet.LastRowNum keeps returning 4 as result instead of 1.... I think this is related to some property bound to the manually-cleaned sheet cells.

Do you have any hint to solve this issue?


回答1:


Am I oversimplifying?

 bool hasContent = false;

 while (sheet.GetRow(rowIndex) != null)
        {
            var row = rows.Current as XSSFRow;
            //all cells are empty, so is a 'blank row'
            if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;  


            hasContent = true;
        }



回答2:


I think it would be wise to use sheet.LastRowNum which should return the amount of rows on the current sheet




回答3:


You can retrieve the number of rows using this code:

public int GetTotalRowCount(bool warrant = false)
{
    IRow headerRow = activeSheet.GetRow(0);
    if (headerRow != null)
    {
        int rowCount = activeSheet.LastRowNum + 1;
        return rowCount;
    }
    return 0;
}



回答4:


Here is a way to get both the actual last row index and the number of physically existing rows:

    public static int LastRowIndex(this ISheet aExcelSheet)
    {
        IEnumerator rowIter = aExcelSheet.GetRowEnumerator();
        return rowIter.MoveNext()
        ? aExcelSheet.LastRowNum
        : -1;
    }

    public static int RowsSpanCount(this ISheet aExcelSheet)
    {
        return aExcelSheet.LastRowIndex() + 1;
    }

    public static int PhysicalRowsCount(this ISheet aExcelSheet )
    {
        if (aExcelSheet == null)
        {
            return 0;
        }

        int rowsCount = 0;
        IEnumerator rowEnumerator = aExcelSheet.GetRowEnumerator();
        while (rowEnumerator.MoveNext())
        {
            ++rowsCount;
        }

        return rowsCount;
    }


来源:https://stackoverflow.com/questions/31344841/npoi-get-excel-row-count-to-check-if-it-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!