【问题标题】:How to read the null cell in NPOI如何读取 NPOI 中的空单元格
【发布时间】:2018-02-28 16:47:37
【问题描述】:

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

【问题讨论】:

  • 你能贴出你用来创建excel文件的代码吗?我尝试了您在阅读我在 Excel 中创建的工作簿之前所做的操作,它运行良好:我没有收到异常并且返回的数值为零。

标签: c# .net excel


【解决方案1】:

我是npoi的新手,但我的印象是你必须检查CellType,然后阅读相应的NumericCellValueStringCellValueBooleanCellValueErrorCellValueCellFormula返回的类型。

例如,如果cell.CellType == CellType.Numeric,那么您将获得cell.NumericCellValue() 的单元格值,如果cell.CellType == CellType.String,那么您将获得cell.StringCellValue() 的单元格值。如果cell.CellType == CellType.Blank 则没有任何值,所以你不能尝试读取一个(否则你会得到一个异常)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多