Browse Source

Merge branch '20250321_sun' of sckj/mz-cloud into master

sckj 1 tháng trước cách đây
mục cha
commit
88aa033d75

+ 45 - 1
ruoyi-modules/mz-gljt/src/main/java/com/ruoyi/gljt/utils/ExcelTemplateUtils.java

@@ -3,12 +3,35 @@ package com.ruoyi.gljt.utils;
 import com.ruoyi.common.core.exception.ServiceException;
 import com.ruoyi.gljt.domain.GljtSqLrExport;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddressList;
 
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 public class ExcelTemplateUtils {
 
+    // 定义数据验证规则的静态常量
+    private static final ValidationRule[] VALIDATION_RULES = {
+            new ValidationRule(4, "HIDDENSHEETNAME!$A$2:$A$3"),    // 性别
+            new ValidationRule(6, "HIDDENSHEETNAME!$B$2:$B$60"),   // 民族
+            new ValidationRule(10, "HIDDENSHEETNAME!$D$2:$D$5"),   // 补贴类别
+            new ValidationRule(11, "HIDDENSHEETNAME!$E$2:$E$4"),   // 健康状况
+            new ValidationRule(13, "HIDDENSHEETNAME!$C$2:$C$3")    // 变动种类
+    };
+
+    // 验证规则实体类
+    private static class ValidationRule {
+        final int col;
+        final String source;
+        ValidationRule(int col, String source) {
+            this.col = col;
+            this.source = source;
+        }
+    }
+
     /**
      * 将数据写入Excel模板
      *
@@ -30,6 +53,9 @@ public class ExcelTemplateUtils {
                     fillRowData(row, data, style);
                 }
 
+                // 批量添加数据验证
+                addBatchValidations(sheet, 1, dataList.size());
+
                 // 调整列宽
                 adjustColumnWidth(sheet);
 
@@ -64,11 +90,29 @@ public class ExcelTemplateUtils {
         createCell(row, colIndex++, data.getXsbtlb(), style);         // 补贴类别
         createCell(row, colIndex++, data.getSndj(), style);           // 健康状况
         createCell(row, colIndex++, data.getYbzje(), style);          // 月补助金额
+
         Cell formulaCell = row.createCell(15);
         formulaCell.setCellStyle(style);
         formulaCell.setCellFormula("IF(AND(LEN(C2)=18,--MID(\"10X98765432\",MOD(SUMPRODUCT(MID(C2,ROW(INDIRECT(\"1:17\")),1)*{7;9;10;5;8;4;2;1;6;3;7;9;10;5;8;4;2}),11)+1,1)=RIGHT(C2,1)*1),\"有效\",\"无效\")");
     }
 
+    /**
+     * 复制一行的所有数据验证到另一行
+     */
+    private static void addBatchValidations(Sheet sheet, int startRow, int endRow) {
+
+        DataValidationHelper dvHelper = sheet.getDataValidationHelper();
+
+        // 批量创建并添加数据验证
+        for (ValidationRule rule : VALIDATION_RULES) {
+            DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint("=" + rule.source);
+            CellRangeAddressList addressList = new CellRangeAddressList(startRow, endRow, rule.col, rule.col);
+            DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
+            validation.setShowErrorBox(false);
+            sheet.addValidationData(validation);
+        }
+    }
+
     private static void createCell(Row row, int colIndex, String value, CellStyle style) {
         Cell cell = row.createCell(colIndex);
         cell.setCellValue(value != null ? value : "");

BIN
ruoyi-modules/mz-gljt/src/main/resources/templates/exportGljtLr.xlsx


BIN
ruoyi-modules/mz-gljt/src/main/resources/templates/exportGljtLr2.xlsx