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

时间:2021-07-19 20:25:11

I've been using the example code given to search the sheet's contents:

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

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();
  }
 }

}

}

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!

任何帮助将不胜感激!

3 个解决方案

#1


3  

Excel stores dates as floating point decimals which is - obviously - different to the way Java handles dates (long values).

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

Apache POI contains a helper class to do date conversion between Java and Excel: DateUtil. This should help (if I got your problem correct).

Apache POI包含一个辅助类,用于在Java和Excel之间进行日期转换:DateUtil。这应该有帮助(如果我的问题正确)。

#2


0  

I noticed that if you read a number from the sheet, even if you read it as a string, you will get the number as decimal.

我注意到如果你从表格中读取一个数字,即使你把它读成一个字符串,你也会得到十进制的数字。

What I do, I keep all data in the cells start with underscore '_', so if I try to read data from any cell as a String, I don't get decimals or anything.

我做什么,我保持单元格中的所有数据都以下划线'_'开头,所以如果我尝试从任何单元格中读取数据作为字符串,我不会得到小数或任何东西。

String cellValue = cell.getValueAsString(); // assuming the cell value is "_01/01/2011"
cellValue = cellValue.substring(1);// this will ignore first character, the '_'

#3


0  

Use DataFormatter class (Apache POI):

使用DataFormatter类(Apache POI):

 DataFormatter df = new DataFormatter();
 String cellValue = df.formatCellValue(Cell cell);

#1


3  

Excel stores dates as floating point decimals which is - obviously - different to the way Java handles dates (long values).

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

Apache POI contains a helper class to do date conversion between Java and Excel: DateUtil. This should help (if I got your problem correct).

Apache POI包含一个辅助类,用于在Java和Excel之间进行日期转换:DateUtil。这应该有帮助(如果我的问题正确)。

#2


0  

I noticed that if you read a number from the sheet, even if you read it as a string, you will get the number as decimal.

我注意到如果你从表格中读取一个数字,即使你把它读成一个字符串,你也会得到十进制的数字。

What I do, I keep all data in the cells start with underscore '_', so if I try to read data from any cell as a String, I don't get decimals or anything.

我做什么,我保持单元格中的所有数据都以下划线'_'开头,所以如果我尝试从任何单元格中读取数据作为字符串,我不会得到小数或任何东西。

String cellValue = cell.getValueAsString(); // assuming the cell value is "_01/01/2011"
cellValue = cellValue.substring(1);// this will ignore first character, the '_'

#3


0  

Use DataFormatter class (Apache POI):

使用DataFormatter类(Apache POI):

 DataFormatter df = new DataFormatter();
 String cellValue = df.formatCellValue(Cell cell);