Java 我想获取excel中显示的日期,如日期类型单元格中的2014年1月12日
下面是我用来获取excel中显示的日期的代码,如:12/1/2014。单元格类型为日期类型。字符串dateFmt=cell.getCellStyle().getDataFormatString()//返回空值 试一试{Java 我想获取excel中显示的日期,如日期类型单元格中的2014年1月12日,java,apache-poi,Java,Apache Poi,下面是我用来获取excel中显示的日期的代码,如:12/1/2014。单元格类型为日期类型。字符串dateFmt=cell.getCellStyle().getDataFormatString()//返回空值 试一试{ String excelFilePath=“D:\\readExcel\\QA 3715 nigbo PFL f525w985.xls”; FileInputStream FileInputStream=新的FileInputStream(excelFilePath); HSSF
String excelFilePath=“D:\\readExcel\\QA 3715 nigbo PFL f525w985.xls”;
FileInputStream FileInputStream=新的FileInputStream(excelFilePath);
HSSF工作簿=新的HSSF工作簿(fileInputStream);
Sheet firstSheet=工作簿。getSheetAt(0);
Row Row=firstSheet.getRow(6);
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
System.out.println(Cell.Cell_TYPE_STRING+”:“+Cell.getStringCellValue());
打破
case Cell.Cell\u类型\u布尔值:
System.out.println(Cell.Cell_TYPE_BOOLEAN+”:“+Cell.getBooleanCellValue());
打破
case Cell.Cell\u类型\u数值:
System.out.println(Cell.Cell_TYPE_NUMERIC+”:“+Cell.getDateCellValue());//获取2014年12月1日星期一00:00:00
if(DateUtil.isCellDateFormatted(cell)){//返回false
System.out.println(“true…”:“+cell.getDateCellValue());
}
if(HSSFDateUtil.isCellDateFormatted(cell)){//返回false
System.out.println(“true…”:“+cell.getDateCellValue());
}
double dv=cell.getNumericCellValue();
日期日期=(日期)HSSFDateUtil.getJavaDate(dv);
字符串dateFmt=cell.getCellStyle().getDataFormatString();
String strValue=new-CellDateFormatter(dateFmt).format(date);//获取null
System.out.println(“标准值:+标准值”);
DataFormatter df=新的DataFormatter();
字符串cellValue=df.formatCellValue(单元格);
System.out.println(“cellValue:+cellValue”);
}
}
}捕获(例外e){
e、 printStackTrace();
}
我不想在外部应用任何日期格式请参阅“谢谢帮助”,但它对我没有用处..我正在获取cell.getCellStyle().getDataFormatString()为null,而工作簿.createDataFormat().getFormat(cell.getCellStyle().getDataFormat())为null。我想获取单元格日期格式,应用相同的格式后,我想从单元格中获取日期值,而不是应用自定义的日期格式。您还没有阅读我第一条评论中的链接答案。至少你还不明白。如果包含日期的单元格格式为默认日期格式(短日期),则文件中仅存储格式id 0xE(14)。因此没有DataFormatString。只有
cell.getCellStyle().getDataFormat()
将为14。Excel
如何显示此日期取决于系统的区域设置,Excel
正在运行。
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();
}