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

时间:2015-02-12 06:48:26

标签: java apache-poi

我想在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

我该如何纠正?

5 个答案:

答案 0 :(得分:8)

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

break之后,您还错过了case个关键字。因此,如果单元格为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;
}

答案 1 :(得分:3)

这是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主题可以回答您的跟进问题。

答案 2 :(得分:0)

尝试此代码。

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

希望是帮助。

答案 3 :(得分:0)

从单元格获取日期后

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

答案 4 :(得分:0)

尝试一下:

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