如何将Excel空白单元格设为NULL

时间:2015-07-08 07:26:06

标签: excel apache-poi

我必须使用Apache POI从excel获得价值。我有两个案例

  1. 必须从Excel单元格中获取Exact值
  2. 获取值
  3. 时应返回NULL值

    对于我使用new DataFormatter().formatCellValue(row.getCell(index))的第一种情况,此方法会从Excel中返回值。但是如果单元格具有空值,则它不会将值返回为NULL。

    我也试过了new DataFormatter().formatCellValue(row.getCell(index, HSSFRow.RETURN_BLANK_AS_NULL))

    请参考随附的截图! Here we have to get the Description value as null Excel with null values

1 个答案:

答案 0 :(得分:1)

DatFormatter的要点是返回一个非空字符串,该字符串类似于Excel为给定单元格显示的内容。对于空单元格,无论是null还是空白,它都是空字符串。这在javadocs for the formatCellValue method中解释了:

  

当传递null或空单元格时,此方法将返回一个空字符串("")

如果要为空单元格设置空值,则应将代码调整为:

// One per workbook is fine, save re-creating each time
DataFormatter formatter = new DataFormatter();

// Per cell
Cell c = row.getCell(index, Row.RETURN_BLANK_AS_NULL);
if (c == null) return null;
return formatter.formatCellValue(c);