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())); } }