如何在Java中读取具有空值的Excel单元格...? [英] How to read Excel cell having null values too in Java...?

查看:66
本文介绍了如何在Java中读取具有空值的Excel单元格...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Apache POI 3.6.我有一列空白.我希望能够阅读它,然后转到下一列.即使我可以解决 NullPointerException 问题,我也无法进入下一个单元格.

I'm using Apache POI 3.6. I've a column which is blank. I would like to be able to read it and then move to the next column. Even if I could resolve the NullPointerException problem I could not get to the next cell.

这是我的代码片段:

HSSFCell cell = row.getCell(c);
String value = null;

switch (cell.getCellType()) {

    case HSSFCell.CELL_TYPE_FORMULA:
        value = "FORMULA value=" + cell.getCellFormula();
        break;

    case HSSFCell.CELL_TYPE_NUMERIC:
        value = "NUMERIC value=" + cell.getNumericCellValue();
        break;

    case HSSFCell.CELL_TYPE_STRING:
        value = "STRING value=" + cell.getStringCellValue();
        break;

    case HSSFCell.CELL_TYPE_BLANK:
        value="";
        break;

    case HSSFCell.CELL_TYPE_ERROR:
        value="error";
        break;

    default:
        break;
}

System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);

我该如何解决我的问题?

How can I resolve my problem?

推荐答案

好吧,您可以在 switch 语句之前检查 null,或者您可以更改对 row.getCell 的调用.检查 POI 的 Javadoc 有两种形式,第一种是您正在使用的形式,第二种有一个附加参数,类型为 Row.MissingCellPolicy,您可以在其中传递一个值,该值会自动将空单元格转换为空白.

Well, you could check for null before your switch statement, or you could change which call to row.getCell you make. Checking the Javadoc for POI there are 2 forms, the first is what you are using, the second has an additional parameter, of the type Row.MissingCellPolicy, where you can pass a value that would automagically transform null cells into blanks.

这篇关于如何在Java中读取具有空值的Excel单元格...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆