当前位置: 首页 > 知识库问答 >
问题:

如何将一个单元格中的所有单元格返回为空。使用ApachePOI的xlsx文件?

澹台硕
2023-03-14

在谷歌搜索StackOverflow之后,我还没有找到与这个问题相关的地方。目前我可以将空白单元格读取为空,但一次只能读取一个单元格。所以我必须写这样的东西:

data.setAddress(row.getCell(10, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(10).getStringCellValue());
    data.setClientType(row.getCell(11, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(11).getNumericCellValue());
    data.setClientType0Charge(row.getCell(12, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(12).getNumericCellValue());
    data.setClientType1Charge(row.getCell(13, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(13).getNumericCellValue());
    data.setCooYears(row.getCell(14, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(14).getNumericCellValue());
    data.setConYears(row.getCell(15, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(15).getNumericCellValue());
    data.setProjectType(row.getCell(16, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(16).getNumericCellValue());
    data.setProjectTypeNotes(row.getCell(17, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(17).getStringCellValue());
    data.setChargeMethod(row.getCell(18, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(18).getNumericCellValue());
    data.setFixedPrice(row.getCell(19, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(19).getNumericCellValue());
    data.setHighTime(row.getCell(20, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(20).getStringCellValue());
    data.setHighPrice(row.getCell(21, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(21).getNumericCellValue());
    data.setPeakTime(row.getCell(22, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(22).getStringCellValue());
    data.setPeakPrice(row.getCell(23, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(23).getNumericCellValue());
    data.setPlainTime(row.getCell(24, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(24).getStringCellValue());
    data.setPlainPrice(row.getCell(25, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(25).getNumericCellValue());
    data.setValleyTime(row.getCell(26, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(26).getStringCellValue());
    data.setValleyPrice(row.getCell(27, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(27).getNumericCellValue());
    data.setServFee(row.getCell(28, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(28).getNumericCellValue());
    data.setServFeePercent(row.getCell(29, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(29).getNumericCellValue());
    data.setServFeeFixed(row.getCell(30, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(30).getNumericCellValue());
    data.setPropertyType(row.getCell(31, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(31).getNumericCellValue());
    data.setPropertyOwner(row.getCell(32, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(32).getStringCellValue());
    data.setPropertyManager(row.getCell(33, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(33).getStringCellValue());
    data.setAcquireMethod(row.getCell(34, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(34).getNumericCellValue());
    data.setAcquireMethodNotes(row.getCell(35, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(35).getStringCellValue());
    data.setSiteAddress(row.getCell(36, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(36).getStringCellValue());
    data.setSiteType(row.getCell(37, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(37).getNumericCellValue());
    data.setSiteTypeNotes(row.getCell(38, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(38).getStringCellValue());

这对我来说不太好,因为我对重复感到不满。我们可以设置行吗。对于整个工作表甚至整个工作簿中的单元格,是否将_BLANK _作为_NULL返回

共有2个答案

邬承悦
2023-03-14

我不知道这样的全局参数,但也许你可以把你的逻辑封装在一个实用类中。例如,要获取字符串值,可以按如下操作(可能添加一些控件)

public static String getStringCellValue (Row row, int cellNum){
   return row.getCell(cellNum, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(cellNum).getStringCellValue();

}

所以你的代码变成了

 data.setAddress(PoiUtils.getStringCellValue (row,10)); 
利俊迈
2023-03-14

您可以在工作簿级别的“缺少单元格”策略中设置:

    XSSFWorkbook book = new XSSFWorkbook("h:\\excel.xlsx");
    XSSFSheet sheet = book.getSheetAt(0);
    XSSFRow row = sheet.getRow(0);
    book.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);

这是因为只有单元格编号的getcell的定义如下(来自POI源代码):

 @Override
    public XSSFCell getCell(int cellnum) {
        return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy());
    }
 类似资料:
  • 我正在使用Poi.jar从excel表输入,想知道如何检查单元格是否为空。 现在我使用下面的代码。

  • 我有一个巨大的excel文件,其中包含大量列,如下所示:- 当我打印excel中的所有值时,我的代码生成的输出是:- 所以,如果我们看看上面的输出,我们可以注意到我留下空白值的单元格没有被POI库拾取。有没有一种方法可以让这些值为空?还是一种识别所呈现的值跳过空白单元格的方法? 请注意:我使用的不是usermodel(org.apache.poi.ss.usermodel),而是一个事件API来处

  • 我的google sheet excel文档包含如下数据 列B和D包含导入函数提供的数据,这些数据存储在不同的文件中。 我想用行中的第一个非空值填充列A,换句话说:所需的结果必须如下所示: 我尝试了ISBLANK函数,但很明显,如果导入了列,那么即使值为空,也不是空的,因此此函数不适用于我的情况。然后,我在两个不同的变体中尝试了查询功能: 1) 但当行包含带数字的单元格时,这种情况下的结果是错误的

  • 我正在使用Apache POI读取零件编号电子表格中的数据。我在我们的数据库中查找零件编号,如果我们有零件的计算机辅助设计图纸,我将零件编号单元格涂成绿色,如果没有,我将其涂成红色。处理完成后,将保存电子表格。我遇到的问题是那列中的每个细胞都是绿色的。我已经完成了代码,查找零件号的逻辑工作正常,确定单元格应该是什么颜色以及设置颜色和填充的逻辑似乎也工作正常。知道我做错了什么吗? 谢谢

  • 我一直在用POI编写一个代码来将数据输入到Excel表中。首先,我需要遍历一组行,我不确定其中有多少行,可能是10或100行。但我发现末尾有一个空白单元格。在那个空白之后,又有一组行。我不担心第二盘。我需要找到第一盘是否已经结束。 这很让人困惑。现在我有5个用户在我的工作表如下所示。在第5个用户之后有一个空白单元格。我试图打印所有的用户,如果有一个空白停止它。

  • 有人能给我一个解决方案,我可以在单元格为空时读取应用到单元格的样式吗? 谢了。