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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM