以下是我用于获取日期的代码,如excel中出现的那样:12/1/2014。单元格类型是日期类型。 String dateFmt = cell.getCellStyle()。getDataFormatString(); //返回null
尝试{
String excelFilePath = "D:\\readExcel\\QA 3715 nigbo PFL F1525W985.xls";
FileInputStream fileInputStream = new FileInputStream(excelFilePath);
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Row row = firstSheet.getRow(6);
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(Cell.CELL_TYPE_STRING + " : "+ cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(Cell.CELL_TYPE_BOOLEAN+" : " + cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(Cell.CELL_TYPE_NUMERIC+" :: "+cell.getDateCellValue());// getting Mon Dec 01 00:00:00 IST 2014
if(DateUtil.isCellDateFormatted(cell)){//returning false
System.out.println("true.... : "+cell.getDateCellValue());
}
if (HSSFDateUtil.isCellDateFormatted(cell)){//returning false
System.out.println("true.... : "+cell.getDateCellValue());
}
double dv = cell.getNumericCellValue();
Date date = (Date) HSSFDateUtil.getJavaDate(dv);
String dateFmt = cell.getCellStyle().getDataFormatString();
String strValue = new CellDateFormatter(dateFmt).format(date);// getting null
System.out.println("strValue : "+strValue);
DataFormatter df = new DataFormatter();
String cellValue = df.formatCellValue(cell);
System.out.println("cellValue : "+cellValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
我不想在外部申请任何日期格式