首页 文章

我正在使用Apache POI阅读excel文件 . 无法阅读日期 . 在excel中,日期格式2017-03-15 6:00(格式格式=自定义)&使用poi,阅读42809.25

提问于
浏览
2

#我正在使用Apache POI阅读excel文件 . 无法阅读日期 . 在excel中,日期格式2017-03-15 6:00(格式格式=自定义)&使用poi,阅读42809.25

cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
    int dataType = cell.getCellType();
    if (dataType == 0) {
        String cellData1 = NumberToTextConverter.toText(cell.getNumericCellValue());
        return cellData1;

    } else {
        String cellData = cell.getStringCellValue();
        return cellData;
    }

2 回答

  • 1

    如果你知道哪一个单元格,即列位置说每行中的2将是一个日期,你可以直接找到 row.getCell(2).getDateCellValue() .

    查看POI的DateUtil类中的方法来处理Excel表格中的日期, DateUtil.isCellDateFormatted()

    或者您可以获得如下所示的日期

    if (DateUtil.isCellDateFormatted(cell))
    {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      cellValue = sdf.format(cell.getDateCellValue());
    
    } catch (ParseException e) {
            e.printStackTrace();
    }
    }
    
  • 1

    感谢Dhaval :)我尝试使用您的代码和其他一些行 . 现在下面的代码对我有用 .

    cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    if (DateUtil.isCellDateFormatted(cell)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                        String cellDate1 = sdf.format(cell.getDateCellValue());
                        return cellDate1;
                    } else {
                        String cellData2 = NumberToTextConverter.toText(cell.getNumericCellValue());
                        return cellData2;
                    }
                } else {
                    String cellData = cell.getStringCellValue();
                    return cellData;
                }
    

相关问题