Java POI - 从 Excel 文件中读取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28471060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java POI - read date from Excel file
提问by Michael Kuan
I want to read the date (yyyy-MM-dd) in Java 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;
}
However, I get the below error:
但是,我收到以下错误:
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
How should I corrected it?
我应该如何纠正它?
回答by rtruszk
Dates cannot be stored in CELL_TYPE_STRING
cell. You should store it in CELL_TYPE_NUMERIC
cell. See herefor details.
日期不能存储在CELL_TYPE_STRING
单元格中。您应该将其存储在CELL_TYPE_NUMERIC
单元格中。有关详细信息,请参见此处。
You also missed break
keyword after first case
. So if cell is Cell.CELL_TYPE_STRING
then also
您break
在 first 之后也错过了关键字case
。所以如果细胞Cell.CELL_TYPE_STRING
也是
System.out.print(cell.getNumericCellValue() + "\t\t");
is called.
叫做。
So it should be:
所以应该是:
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;
}
回答by Gaurav Gupta
This is direct pick from Apache POI tutorial, you make like to visit and get more details.
这是Apache POI 教程的直接选择,您喜欢访问并获得更多详细信息。
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();
}
Formatting date: Thisthread may answer your follow up question.
格式化日期:此线程可能会回答您的后续问题。
回答by superup
Try this code.
试试这个代码。
Date date = row.getCell(0).getDateCellValue(); //Get Date
cell.getDateCellValue(); //Get date time
System.out.println(date.getTime());
Hope is help.
希望是帮助。