从Excel读取数据单元格值

时间:2019-12-12 09:03:24

标签: java apache-poi exceldatareader excel-dates

我正在读取Excel文件,并试图找出哪些单元格值是日期值。现在,我尝试使用Apache POI SS用户模型的getCellType进行此操作,但问题是我的excel即使对于包含Date值的单元格也返回String值。

enter image description here

有没有解决这个问题的方法?

下面是我的参考代码。

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

0 个答案:

没有答案