如何使用Apache POI在Excel工作表中搜索特定日期?

Jsk*_*Jsk 5 java excel poi-hssf apache-poi

我一直在使用给出的示例代码来搜索工作表的内容:

Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
 for (Cell cell : row) {
  CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum());
  System.out.print(cellRef.formatAsString());
  System.out.print(" - ");

  switch(cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
        System.out.println(cell.getRichStringCellValue().getString());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        if(DateUtil.isCellDateFormatted(cell)) {
          System.out.println(cell.getDateCellValue());
        } else {
          System.out.println(cell.getNumericCellValue());
        }
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        System.out.println(cell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        System.out.println(cell.getCellFormula());
        break;
      default:
        System.out.println();
  }
 }
Run Code Online (Sandbox Code Playgroud)

}

But can't find a way to properly compare the dates extracted from the sheet with a given date. I've tried using a data validation and also formatting the date to a string and including further conditional statements but nothing has worked.

seems like a relatively simple problem and necessary to but havent been able to fix it yet?

any help would be greatly appreciated!

And*_*s_D 3

Excel 将日期存储为浮点小数,这显然与 Java 处理日期(long值)的方式不同。

Apache POI 包含一个帮助程序类来在 Java 和 Excel 之间进行日期转换:DateUtil. 这应该有帮助(如果我正确地解决了你的问题)。