From 9a41d07f14d92697071738897a2fb75b96454456 Mon Sep 17 00:00:00 2001 From: wsm <2746563060@qq.com> Date: Tue, 30 Jun 2026 16:25:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(excel):=20=E4=BF=AE=E5=A4=8D=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=A0=BC=E5=BC=8F=E4=B8=8D=E5=85=BC=E5=AE=B9=E5=92=8C?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=BC=82=E5=B8=B8=E6=9C=AA=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增弹性日期解析,兼容 yyyy/MM/dd、yyyy/M/d、yyyy.M.d 等 Excel 常见日期变体 - 系统异常时调用 handleError 上报错误,防止前端误报导入成功 Co-Authored-By: Claude Opus 4.7 --- .../util/excel/ExcelAnnotationUtils.java | 34 ++++++++++++++++++- .../util/excel/ExcelImportListener.java | 1 + 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelAnnotationUtils.java b/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelAnnotationUtils.java index 09526ba..737c225 100644 --- a/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelAnnotationUtils.java +++ b/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelAnnotationUtils.java @@ -234,7 +234,7 @@ public final class ExcelAnnotationUtils { } else if (type == Date.class) { String format = StringUtils.isNotEmpty(col.databaseFormat()) ? col.databaseFormat() : col.format(); if (StringUtils.isNotEmpty(format)) { - field.set(obj, new SimpleDateFormat(format).parse(resolved)); + field.set(obj, parseDateFlexible(resolved, format)); } } else if (type == LocalDateTime.class) { String format = col.format(); @@ -258,6 +258,38 @@ public final class ExcelAnnotationUtils { } } + /** + * 弹性日期解析:依次尝试精确格式、分隔符归一化、单数字月/日格式, + * 兼容 Excel 中 "2026/6/12"、"2026-6-12"、"2026/06/12" 等常见变体。 + */ + private static Date parseDateFlexible(String value, String format) throws java.text.ParseException { + // 1. 精确格式 + try { + return new SimpleDateFormat(format).parse(value); + } catch (java.text.ParseException ignored) { + } + + // 2. 分隔符归一化(/ → -,. → -) + String normalizedValue = value.replace('/', '-').replace('.', '-'); + try { + return new SimpleDateFormat(format).parse(normalizedValue); + } catch (java.text.ParseException ignored) { + } + + // 3. 单数字月/日宽松格式(MM→M, dd→d) + String relaxedFormat = format.replace("MM", "M").replace("dd", "d"); + try { + return new SimpleDateFormat(relaxedFormat).parse(value); + } catch (java.text.ParseException ignored) { + } + try { + return new SimpleDateFormat(relaxedFormat).parse(normalizedValue); + } catch (java.text.ParseException ignored) { + } + + throw new java.text.ParseException("Unparseable date: \"" + value + "\" with format: \"" + format + "\"", 0); + } + // ---- 私有方法 ---- private static Object getCellValue(ColumnMeta col, Object obj) { diff --git a/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelImportListener.java b/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelImportListener.java index 2094a5b..8a516b9 100644 --- a/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelImportListener.java +++ b/jeecg-boot-module/jeecg-module-tools/src/main/java/org/jeecg/common/util/excel/ExcelImportListener.java @@ -146,6 +146,7 @@ public class ExcelImportListener implements ReadListener } catch (Exception e) { skipCount[0]++; log.error("【Excel导入】行数据转换发生 system 异常, row: {}", rowIndex, e); + handleError(rowIndex + 1, Collections.singletonList("数据转换异常: " + e.getMessage())); } }