From 6db05b0940aadcc027f03b99f52dcb8a512c8c6d Mon Sep 17 00:00:00 2001 From: gaoming Date: Tue, 23 Jun 2020 11:33:15 +0800 Subject: [PATCH] 优化excel读日期 --- build.gradle | 2 +- src/main/java/com/taover/util/UtilExcel.java | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index ecfdbea..0f9a580 100644 --- a/build.gradle +++ b/build.gradle @@ -59,7 +59,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.105' + version '1.1.106' 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 9124ba4..a998395 100644 --- a/src/main/java/com/taover/util/UtilExcel.java +++ b/src/main/java/com/taover/util/UtilExcel.java @@ -10,11 +10,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.util.ZipSecureFile; 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.FillPatternType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -449,10 +451,7 @@ public class UtilExcel { */ System.out.println(cell.getCellStyle().getDataFormat()); short format = cell.getCellStyle().getDataFormat(); - if (format==28 || format==22||format==31 || format == 58 || format==14 || format==57 || format==32 || format==20 || format==176 ) { - - // 如果是date类型则 ,获取该cell的date值 - //return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); + if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = null; if(format == 14 || format == 31 || format == 57 || format == 58 ){ //日期 @@ -513,7 +512,7 @@ public class UtilExcel { System.out.println(df.format(3.3300)); - String filepath = "C:\\Users\\EDZ\\Desktop\\ttt.xls"; + String filepath = "C:\\Users\\EDZ\\Desktop\\date - 副本.xlsx"; List> data = null; try { -- libgit2 0.21.2