码迷,mamicode.com
首页 > 其他好文 > 详细

POI 读取 excel日期格式问题

时间:2019-09-01 16:15:56      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:val   lld   lse   ret   exce   private   bool   att   getc   

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());
    }
}

POI 读取 excel日期格式问题

标签:val   lld   lse   ret   exce   private   bool   att   getc   

原文地址:https://www.cnblogs.com/zhehang/p/11442467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!