From 7fa02f9b28991ce3189aea3629cb7a3f3fb3b122 Mon Sep 17 00:00:00 2001
From: longz <longzhaoz@qq.com>
Date: 星期一, 20 七月 2026 22:37:24 +0800
Subject: [PATCH] fix: 保持导入模板文本格式

---
 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
index b0533b4..09130cf 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
@@ -718,7 +718,7 @@
         for (Object[] os : fields)
         {
             Excel excel = (Excel) os[1];
-            String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
+            String key = StringUtils.format("data_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType());
             if (!styles.containsKey(key))
             {
                 CellStyle style = wb.createCellStyle();
@@ -734,6 +734,10 @@
                 style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
                 style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                 style.setFillForegroundColor(excel.backgroundColor().getIndex());
+                if (ColumnType.STRING == excel.cellType())
+                {
+                    style.setDataFormat(wb.createDataFormat().getFormat("@"));
+                }
                 Font dataFont = wb.createFont();
                 dataFont.setFontName("Arial");
                 dataFont.setFontHeightInPoints((short) 10);
@@ -756,10 +760,10 @@
         cell.setCellValue(attr.name());
         setDataValidation(attr, row, column);
         cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
+        sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}",
+                attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
         if (isSubList())
         {
-            // 填充默认样式,防止合并单元格样式失效
-            sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
             if (attr.needMerge())
             {
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
@@ -889,7 +893,8 @@
                     CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
                     sheet.addMergedRegion(cellAddress);
                 }
-                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
+                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}",
+                        attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
 
                 // 用于读取对象中的属性
                 Object value = getTargetValue(vo, field, attr);

--
Gitblit v1.7.1