From 9e5d075b0d6b5d4d1c8b7a7a0e0c1415171e6bd1 Mon Sep 17 00:00:00 2001 From: SPPan Date: Sat, 27 Nov 2021 22:32:15 +0800 Subject: [PATCH] =?UTF-8?q?POI=E8=AF=BB=E5=8F=96EXCEL=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E4=B8=BA1899=E5=B9=B4=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/poi/excel/cell/values/NumericCellValue.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java index 951af97c2..bf160730e 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java @@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.ExcelDateUtil; import cn.hutool.poi.excel.cell.CellValue; +import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.NumberToTextConverter; @@ -24,7 +25,7 @@ public class NumericCellValue implements CellValue { * * @param cell {@link Cell} */ - public NumericCellValue(Cell cell){ + public NumericCellValue(Cell cell) { this.cell = cell; } @@ -36,8 +37,13 @@ public class NumericCellValue implements CellValue { if (null != style) { // 判断是否为日期 if (ExcelDateUtil.isDateFormat(cell)) { + // 1899年写入会导致数据错乱,读取到1899年证明这个单元格的信息不关注年月日 + Date dateCellValue = cell.getDateCellValue(); + if ("1899".equals(DateUtil.format(dateCellValue, "yyyy"))) { + return DateUtil.format(dateCellValue, style.getDataFormatString()); + } // 使用Hutool的DateTime包装 - return DateUtil.date(cell.getDateCellValue()); + return DateUtil.date(dateCellValue); } final String format = style.getDataFormatString();