使用Java POI读取excel并将String值设置为Integer

时间:2018-01-04 12:28:48

标签: java excel apache-poi

我尝试使用java poi读取excel数据并尝试存储在数据库中。

我从excel中读取了值

Integer.parseInt(formatter.formatCellValue(row.getCell(columnNum))) 

其中,excel可能在列中包含空值。所以它抛出错误

java.lang.NumberFormatException: For input string: "" 

尝试将excel值设置为Integer,如

riskVo.setProbability(Integer.parseInt(formatter.formatCellValue(row.getCell(columnNum))));

注意:概率是一种整数数据类型。

但是如果列不为空,它接受值。

这是excel文件。

enter image description here 错误发生在包含空列的第2行第2列中。如果列具有值,则它可以正常工作。

1 个答案:

答案 0 :(得分:1)

请注意,CompilerGenerated""之间存在差异。由于空字符串NULL""不同,因此您获得异常的是。

例如:

NULL

但是,

String s  = "";
s.length ()// it will return 0

因此,如果String s = null; s.length ()// throw NPE. 不是String,您可以先检查一下,然后检查它是否为空。

null