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

How to get an Excel Blank Cell As NULL

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

  1. 必须从 Excel 单元格中获取精确值
  2. 获取值
  3. 时,NULL值应为return

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

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

请参考随附的屏幕截图! 这里我们要获取Description值为null Excel 具有空值

DatFormatter is to give you back a non-null string that looks like what Excel would have displayed for the given cell. For an empty cell, be that null or blank, that's an empty string. That's explained in the javadocs for the formatCellValue method的点数:

When passed a null or blank cell, this method will return an empty String ("")

如果您想为空单元格设置空值,您应该将代码调整为:

// 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);