用POI导入excel数据时,日期格式(如2018/7/7)数据默认会被解析成double格式,解决方法如下:
package com.test.util;
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.DateUtil;
public class ExcelUtil {
/**
* 解析POI导入Excel中日期格式数据
* @param currentCell
* @return currentCellValue
*/
public static String importByExcelForDate(Cell currentCell) {
String currentCellValue = "";
// 判断单元格数据是否是日期
if ("yyyy/mm;@".equals(currentCell.getCellStyle().getDataFormatString())
|| "m/d/yy".equals(currentCell.getCellStyle().getDataFormatString())
|| "yy/m/d".equals(currentCell.getCellStyle().getDataFormatString())
|| "mm/dd/yy".equals(currentCell.getCellStyle().getDataFormatString())
|| "dd-mmm-yy".equals(currentCell.getCellStyle().getDataFormatString())
|| "yyyy/m/d".equals(currentCell.getCellStyle().getDataFormatString())) {
if (DateUtil.isCellDateFormatted(currentCell)) {
// 用于转化为日期格式
Date d = currentCell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
currentCellValue = formater.format(d);
}
} else {
// 不是日期原值返回
currentCellValue = currentCell.toString();
}
return currentCellValue;
}
}