From 60ed22eeed01325a26a56c4e9dc848006410ac7f Mon Sep 17 00:00:00 2001 From: gaoming Date: Wed, 18 Dec 2019 17:45:09 +0800 Subject: [PATCH] 获取自定义日期格式 --- build.gradle | 2 +- src/main/java/com/taover/util/UtilExcel.java | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index a37b3f2..eed7f8b 100644 --- a/build.gradle +++ b/build.gradle @@ -59,7 +59,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.29' + version '1.1.30' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilExcel.java b/src/main/java/com/taover/util/UtilExcel.java index dca5e0c..4072c6d 100644 --- a/src/main/java/com/taover/util/UtilExcel.java +++ b/src/main/java/com/taover/util/UtilExcel.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -15,6 +16,7 @@ import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -331,7 +333,15 @@ public class UtilExcel { if(currCellType.compareTo(CellType.STRING) == 0){ return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ - return df.format(cell.getNumericCellValue()); + + //if (DateUtil.isCellDateFormatted(cell)) { + if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { + // 如果是date类型则 ,获取该cell的date值 + return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); + } else { // 纯数字 + // return String.valueOf(cell.getNumericCellValue()); + return df.format(cell.getNumericCellValue()); + } }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ return cell.getBooleanCellValue(); }else if(currCellType.compareTo(CellType.FORMULA) == 0){ @@ -413,7 +423,7 @@ public class UtilExcel { public static void main(String args[]){ //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; - String filepath = "C:\\Users\\EDZ\\Desktop\\榴莲1 (5).xlsx"; + String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xlsx"; List> data = null; try { data = UtilExcel.readExcel(filepath); -- libgit2 0.21.2