Java POI - 从 Excel 文件中读取日期

Java POI - read date from Excel file

我想在 Java POI 中读取日期 (yyyy-MM-dd)。

Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);

switch(cell.getCellType()){
    case Cell.CELL_TYPE_STRING:
         if(DateUtil.isCellDateFormatted(cell)){  [ERROR HERE]
              System.out.print(cell.getDateCellValue() + "\t\t");
         }else{
              System.out.print(cell.getStringCellValue() + "\t\t");
         }
    case Cell.CELL_TYPE_NUMERIC:
        System.out.print(cell.getNumericCellValue() + "\t\t");
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        System.out.print(cell.getBooleanCellValue() + "\t\t");
        break;
}

但是,我收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:882)
at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:220)
at org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(DateUtil.java:495)
at others.ReadDateTime.main(ReadDateTime.java:44)
Java Result: 1

我该如何修改?

日期不能存储在 CELL_TYPE_STRING 单元格中。您应该将其存储在 CELL_TYPE_NUMERIC 单元格中。有关详细信息,请参阅 here

您还错过了第一个 case 之后的 break 关键字。因此,如果单元格是 Cell.CELL_TYPE_STRING,那么也是

System.out.print(cell.getNumericCellValue() + "\t\t");

被调用。

所以应该是:

switch(cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        System.out.print(cell.getStringCellValue() + "\t\t");
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            System.out.print(dateFormat.format(cell.getDateCellValue()) + "\t\t");
        } else {
            System.out.print(cell.getNumericCellValue() + "\t\t");
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        System.out.print(cell.getBooleanCellValue() + "\t\t");
        break;
}

这是直接从Apache POI tutorial挑选的,你喜欢访问并了解更多详细信息。

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

格式化日期:This 帖子可能会回答您的后续问题。

试试这个代码。

Date date = row.getCell(0).getDateCellValue(); //Get Date
cell.getDateCellValue(); //Get date time
System.out.println(date.getTime());

希望是帮助。

从单元格获取日期后

case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.println(cell.getDateCellValue());
                } else {
                    System.out.println(cell.getNumericCellValue());
                }
                break;

然后像这样简单地格式化日期:

Date mydate=cell.getDateCellValue();
                            SimpleDateFormat fo= new SimpleDateFormat("MM/dd/yyyy");
                            System.out.println(fo.format(mydate));

试试这个:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;

// ...

Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
if (cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA) {
    String cellValue = String.valueOf(cell.getNumericCellValue());
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = cell.getDateCellValue();
        cellValue = df.format(date);
    }
    System.out.println(cellValue);
}