使用Java替换Excel中的空白数字单元格
我正在迭代Excel电子表格,以使用Java获取值
每当遇到空白字符串单元格时,我都可以用null替换此空白单元格
但是,在尝试对数值单元格执行相同操作时,我无法做到这一点
下面是我的代码示例
case 3:
String name;
if ("".equals(cell.getStringCellValue())) {
userType = "null";
} else {
userType = cell.getStringCellValue();
}
break;
case 4:
int hits;
hits = (int) cell.getNumericCellValue();
if(Integer.valueOf(hits) == 0) {
hits = 1;
}
有人能告诉我需要做什么来检测单元格为空并用数字1替换该值吗
# 1 楼答案
您可以检查是否等于
" "
,然后分配1。 您还可以尝试解析为int并捕获ifnotANumberException
,然后分配1您现在正在做的是获取int值,而它甚至可能不是int,然后检查是否为0,但
"null" != null != 0