首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Apache POI检查excel单元格是否为空?

如何使用Apache POI检查excel单元格是否为空?
EN

Stack Overflow用户
提问于 2013-04-02 20:42:54
回答 12查看 133.9K关注 0票数 44

我正在使用Poi.jar从excel工作表中获取输入,想知道如何检查单元格是否为空。

现在我正在使用下面的代码。

代码语言:javascript
复制
cell = myRow.getCell(3);
if (cell != null) {
    cell.setCellType(Cell.CELL_TYPE_STRING);

    //System.out.print(cell.getStringCellValue() + "\t\t");
    if (cell.getStringCellValue() != "")
        depend[p] = Integer.parseInt(cell.getStringCellValue());

    }
}
EN

回答 12

Stack Overflow用户

发布于 2013-04-03 13:21:17

如果您使用的是Apache POI 4.x,则可以使用:

代码语言:javascript
复制
 Cell c = row.getCell(3);
 if (c == null || c.getCellType() == CellType.Blank) {
    // This cell is empty
 }

对于早于迁移到CellType枚举之前的Apache POI3.x版本,它是:

代码语言:javascript
复制
 Cell c = row.getCell(3);
 if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
    // This cell is empty
 }

但是,不要忘记检查Row是否为null --如果从未使用过没有使用过单元格或样式的行,则该行本身可能为null!

票数 67
EN

Stack Overflow用户

发布于 2015-11-16 17:38:27

Gagravarr的回答非常好!

检查excel单元格是否为空

但是,如果假设包含空字符串 ("")的单元格也为空,则需要一些额外的代码。如果单元格被编辑后没有正确清除,就会发生这种情况(有关如何正确清除单元格的信息,请参见下文)。

我写了一个帮助器来检查XSSFCell是否为空(包括空字符串)。

代码语言:javascript
复制
 /**
 * Checks if the value of a given {@link XSSFCell} is empty.
 * 
 * @param cell
 *            The {@link XSSFCell}.
 * @return {@code true} if the {@link XSSFCell} is empty. {@code false}
 *         otherwise.
 */
public static boolean isCellEmpty(final XSSFCell cell) {
    if (cell == null) { // use row.getCell(x, Row.CREATE_NULL_AS_BLANK) to avoid null cells
        return true;
    }

    if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return true;
    }

    if (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getStringCellValue().trim().isEmpty()) {
        return true;
    }

    return false;
}

注意更新的POI版本

他们首先从3.15 Beta 3版本将getCellType()更改为getCellTypeEnum(),然后从4.0版本移回getCellType()

>= 3.15 Beta 3

代码语言:javascript
复制
- Use `CellType.BLANK` and `CellType.STRING` instead of `Cell.CELL_TYPE_BLANK` and `Cell.CELL_TYPE_STRING`

Version < 4.0

  • Version >= 3.15 Beta 3

代码语言:javascript
复制
- Use `Cell.getCellTypeEnum()` instead of `Cell.getCellType()`

但更好的是double check yourself,因为他们计划在未来的版本中将其改回。

示例

此JUnit测试显示需要额外的空检查的情况。

场景:在Java程序中更改单元格的内容。稍后,在同一个Java程序中,检查单元格是否为空。如果isCellEmpty(XSSFCell cell)函数不检查空字符串,则测试将失败。

代码语言:javascript
复制
@Test
public void testIsCellEmpty_CellHasEmptyString_ReturnTrue() {
    // Arrange
    XSSFCell cell = new XSSFWorkbook().createSheet().createRow(0).createCell(0);

    boolean expectedValue = true;
    boolean actualValue;

    // Act
    cell.setCellValue("foo");
    cell.setCellValue("bar");
    cell.setCellValue(" ");
    actualValue = isCellEmpty(cell);

    // Assert
    Assert.assertEquals(expectedValue, actualValue);
}

此外:正确清除单元格

以防万一有人想知道如何正确清除单元格的内容。有两种方法可以将其存档(我建议使用方法1)。

代码语言:javascript
复制
// way 1
public static void clearCell(final XSSFCell cell) {
    cell.setCellType(Cell.CELL_TYPE_BLANK);
}

// way 2
public static void clearCell(final XSSFCell cell) {
    String nullString = null;
    cell.setCellValue(nullString); 
}

为什么是方式1?显式比隐式更好(谢谢,Python)

方法1:将单元格类型显式设置回blank

方法2:由于将单元格值设置为null字符串时的副作用,将单元格类型隐式设置回为blank

有用的资源

winklerrr

票数 31
EN

Stack Overflow用户

发布于 2018-04-25 01:04:57

从Apache POI 3.17开始,您必须使用枚举检查单元格是否为空:

代码语言:javascript
复制
import org.apache.poi.ss.usermodel.CellType;

if(cell == null || cell.getCellTypeEnum() == CellType.BLANK) { ... }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15764417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档