POI 读取 excel日期格式问题

private static String getValue(XSSFCell cell) {
if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
      return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { 
   //20180622,支持日期格式
  if(HSSFDateUtil.isCellDateFormatted(cell)){
       Date d = (Date) cell.getDateCellValue();
       DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//HH:mm:ss
      return df2.format(d);
 }
//数字
else{
       //使用DecimalFormat对double进行了格式化,随后使用format方法获得的String就是你想要的值了。
      DecimalFormat df = new DecimalFormat("0");
       return String.valueOf(df.format(cell.getNumericCellValue()));
}

} else {
      return String.valueOf(cell.getStringCellValue());
   }
}

private static String getValue(HSSFCell hssfCell) {
      if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
           return String.valueOf(hssfCell.getBooleanCellValue());
      } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
//20180622,支持日期格式
     if(HSSFDateUtil.isCellDateFormatted(hssfCell)){
          Date d = (Date) hssfCell.getDateCellValue();
          DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//HH:mm:ss
          return df2.format(d);
}
//数字
     else{
             //使用DecimalFormat对double进行了格式化,随后使用format方法获得的String就是你想要的值了。
              DecimalFormat df = new DecimalFormat("0");
             return String.valueOf(df.format(hssfCell.getNumericCellValue()));
      }
} else {
       return String.valueOf(hssfCell.getStringCellValue());
    }
}

posted on 2019-09-01 15:33  哲航  阅读(4694)  评论(0编辑  收藏  举报