0

我正在阅读一个 excel 文件,并试图找出哪些单元格值是日期值。现在,我尝试使用 Apache POI SS 用户模型的 getCellType 来执行此操作,但问题是我的 excel 即使对于包含日期值的单元格也返回字符串值。

在此处输入图像描述

有没有办法解决这个问题?

下面是我的代码供参考。

if (wb != null) {
            sh = wb.getSheetAt(0);
            CellStyle cellStyle = wb.createCellStyle();
            CreationHelper createHelper = wb.getCreationHelper();
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD-MM-YYYY HH:MM"));
            System.out.println("Printing columns header.......");
            for (int r = sh.getFirstRowNum()+1; r <= 10; r++) { //this for loop is iterating through all the rows of excel
                Row currentRow = sh.getRow(r); //get r'th row
                for (int c=currentRow.getFirstCellNum(); c<currentRow.getLastCellNum(); c++) { //this for loop is iterating through columns 4 to 7 of r'th row
                    Cell currentCell = currentRow.getCell(c); //get the c'th column
                    if (currentCell != null) {
                        CellType currentCellType = currentCell.getCellType();
                        if (currentCellType != null) {
                            System.out.print(currentCell.getCellType() + ":");
                            printCellValue(currentCellType, currentCell);
                        }
                    }

//                  if (DateUtil.isCellDateFormatted(currentCell)) {
//                      System.out.println("Current Cell value: " + currentCell.getDateCellValue());
//                  }
                }
                System.out.println();
            }
        }
4

0 回答 0