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


i'm reading xlsx file using npoi lib, c#. need extract of excel columns , save extracted values kind of data structure.

i can read file , values 2nd (the first 1 contains headers) last row following code:

... workbook = new xssfworkbook(fs); sheet = (xssfsheet)workbook.getsheetat(0); .... int rowindex = 1;  //--- skip first row (index == 0) contains text headers while (sheet.getrow(rowindex) != null) {     (int = 0; < 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 value custom data structure     }     rowindex++; } 

what i'd check if excel file empty or if has 1 row in order handle issue , display message

if run code against excel file 1 row (headers), breaks on

cell.setcelltype(celltype.string); //--- here cell null 

with following error:

object reference not set instance of object. 

i tried row count with

sheet.lastrownum 

but not return right number of rows. example, have created excel 5 rows (1xheader + 4xdata), code reads excel values. on same excel have removed 4 data rows , have launched again code on excel file. sheet.lastrownum keeps returning 4 result instead of 1.... think related property bound manually-cleaned sheet cells.

do have hint solve issue?

i think wise use sheet.lastrownum should return amount of rows on current sheet


Comments