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

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

我尝试使用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))));

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

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

这是excel文件。

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

java excel apache-poi
1个回答
1
投票

请注意,""NULL之间存在差异。你得到的例外是因为空字符串"",它与NULL不同。

例如 :

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

但,

String s  = null;
s.length ()// throw NPE.

所以,你可能首先检查,如果String不是null然后检查它是否为空。

if(str != null && !str.isEmpty()) {
© www.soinside.com 2019 - 2024. All rights reserved.