Java 从excel中读取数据单元格值

Java 从excel中读取数据单元格值,java,apache-poi,exceldatareader,excel-dates,Java,Apache Poi,Exceldatareader,Excel Dates,我正在读一个excel文件,试图找出哪些单元格值是日期值。现在,我尝试使用ApachePOI SS用户模型的getCellType执行此操作,但问题是,我的excel即使对于包含日期值的单元格也返回字符串值 有办法解决这个问题吗 下面是我的代码供参考 if (wb != null) { sh = wb.getSheetAt(0); CellStyle cellStyle = wb.createCellStyle(); Cr

我正在读一个excel文件,试图找出哪些单元格值是日期值。现在,我尝试使用ApachePOI 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();
            }
        }
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(“打印列标题”);

对于(int r=sh.getFirstRowNum()+1;r,请分享您迄今为止尝试的代码片段?已在QQ问题中添加了我的代码。我想告诉您的一件事是..如果它是excel文件,那么您在原始文件列中看到的日期可能(肯定会)当它作为文件发送时被格式化。在这种情况下,您可能不希望DateFormatter像在普通代码中格式化字符串一样工作。请尝试使用csv文件执行相同的代码。如果日期为DD-MM-YYYY HH:MM,则此格式的代码可以正常工作(我仍在浏览您的代码。)@miiiii-我的挑战不是为日期设置正确的格式,而是在getCellType方法返回字符串而不是日期值所需的数字时识别哪些单元格包含日期。