如何读取NPOI中的空单元格

问题描述 投票:2回答:1

我使用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()编写。谢谢。

c# .net excel
1个回答
1
投票

我是npoi的新手,但我的印象是你必须检查CellType,然后根据返回的类型阅读适当的NumericCellValueStringCellValueBooleanCellValueErrorCellValueCellFormula

例如,如果cell.CellType == CellType.Numeric然后你用cell.NumericCellValue()获得单元格值,如果cell.CellType == CellType.String那么你得到cell.StringCellValue()的单元格值。如果cell.CellType == CellType.Blank那么没有价值所以你不能试图读一个(或者你会得到一个例外)。

© www.soinside.com 2019 - 2024. All rights reserved.