首页 > 其他 > 详细

POI 读取 excel日期格式问题

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

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日期格式问题

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!