我使用NPOI来读取excel文件。如果单元格是数字,则代码为
movie.YourRating = watchListSheet.GetRow(rowNumber).GetCell(newIMDBformat["Your Rating"], MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue;
但是,如果单元格为空,则返回异常:
"Can not get numerical value from text cell."
将NumericCellValue更改为StringCellValue后,它也会返回异常。我想知道我应该用什么来读取空单元格。 excel文件由SetCellValue()编写。谢谢。
我是npoi的新手,但我的印象是你必须检查CellType
,然后根据返回的类型阅读适当的NumericCellValue
,StringCellValue
,BooleanCellValue
,ErrorCellValue
或CellFormula
。
例如,如果cell.CellType == CellType.Numeric
然后你用cell.NumericCellValue()
获得单元格值,如果cell.CellType == CellType.String
那么你得到cell.StringCellValue()
的单元格值。如果cell.CellType == CellType.Blank
那么没有价值所以你不能试图读一个(或者你会得到一个例外)。