1、首先判断cell类型是否为日期型
/**
* 判断cell类型是否为日期型
* @param Cell cell
* @return true 是日期类型 false 否,不是日期类型
* @throws Exception
*/
private booleanisCellDateFormatted(Cell cell) {
if (cell == null) return false;
boolean isDate = false;
double d =cell.getNumericCellValue();
if ( DateUtil.isValidExcelDate(d) ){
CellStyle style =cell.getCellStyle();
if(style==null) return false;
int i = style.getDataFormat();
String f =style.getDataFormatString();
isDate = DateUtil.isADateFormat(i,f);
}
return isDate;
}
2、其次,如果单元格的日期类型,想转成字符串比如“2017-04-12” 则可以参加如下方法
/**
* 从日期型cell单元格中得到字符串格式化日期
* @param Cell cell
* @return 格式化好的日期字符串
* @throws Exception
*/
private String dateString(Cell cell){
String datestr = "";
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date =cell.getDateCellValue();
DateFormat formater = newSimpleDateFormat("yyyy-MM-dd");
datestr =formater.format(date);
}
return datestr;
}
—戴卡五楼