From 640312c745419a7392e33a6933a3eaed3d3c5a70 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 31 May 2020 16:45:37 +0800 Subject: [PATCH] fix sax for date bug --- CHANGELOG.md | 3 ++- .../cn/hutool/poi/excel/sax/Excel03SaxReader.java | 2 +- .../cn/hutool/poi/excel/sax/ExcelSaxUtil.java | 15 +++++++++++++-- .../hutool/poi/excel/test/ExcelSaxReadTest.java | 5 +++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c8267313..7c56ae8dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,8 @@ ### Bug修复 * 【core 】 修复SimpleCache死锁问题(issue#I1HOKB@Gitee) * 【core 】 修复SemaphoreRunnable释放问题(issue#I1HLQQ@Gitee) -* 【poi 】 修复Sax方式读取Excel行号错误问题(issue#882@Gitee) +* 【poi 】 修复Sax方式读取Excel行号错误问题(issue#882@Github) +* 【poi 】 修复Sax方式读取Excel日期类型数据03和07不一致问题(issue#I1HL1C@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java index da8d6152b..0a753e65c 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java @@ -308,7 +308,7 @@ public class Excel03SaxReader extends AbstractExcelSaxReader i value = numrec.getValue(); } else if (formatString.contains(StrUtil.SLASH) || formatString.contains(StrUtil.COLON)) { //日期 - value = formatListener.formatNumberDateCell(numrec); + value = ExcelSaxUtil.getDateValue(numrec.getValue()); } else { final double doubleValue = numrec.getValue(); final long longPart = (long) doubleValue; diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java index da55c4a2a..e475e2f25 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java @@ -165,6 +165,17 @@ public class ExcelSaxUtil { } } + /** + * 获取日期 + * + * @param value 单元格值 + * @return 日期 + * @since 5.3.6 + */ + public static DateTime getDateValue(String value) { + return getDateValue(Double.parseDouble(value)); + } + /** * 获取日期 * @@ -172,8 +183,8 @@ public class ExcelSaxUtil { * @return 日期 * @since 4.1.0 */ - private static DateTime getDateValue(String value) { - return DateUtil.date(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(value), false)); + public static DateTime getDateValue(double value) { + return DateUtil.date(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value, false)); } /** diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java index 3ff449bde..0810df697 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java @@ -103,8 +103,9 @@ public class ExcelSaxReadTest { } @Test + @Ignore public void dateReadTest(){ - ExcelUtil.readBySax("d:/test/sax_test.xls", 0, (RowHandler) (i, i1, list) -> + ExcelUtil.readBySax("d:/test/sax_date_test.xlsx", 0, (i, i1, list) -> Console.log(StrUtil.join(", ", list))); - }; + } }