Browse Source

feat:
导出时自动合并单元格

18339543638 2 years ago
parent
commit
23f643281b

+ 26 - 0
tr-modules-api/tr-module-export-api/src/main/java/cn/tr/module/export/annotation/ExcelCellMerge.java

@@ -0,0 +1,26 @@
+package cn.tr.module.export.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * @Interface : ExcelCellMerge
+ * @Description :
+ * @Author : LF
+ * @Date: 2023年11月08日
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface ExcelCellMerge {
+    /**
+     * 是否自动合并
+     * @return
+     */
+    boolean autoMerge() default true;
+
+    /**
+     * 当单元格内容为空时是否合并
+     * @return
+     */
+    boolean mergeBlank() default false;
+}

+ 2 - 0
tr-modules-api/tr-module-export-api/src/main/java/cn/tr/module/export/annotation/ExcelPropertySupport.java

@@ -28,4 +28,6 @@ public @interface ExcelPropertySupport {
      * 导出模板批注
      */
     String comment() default "";
+
+    ExcelCellMerge merge();
 }

+ 36 - 0
tr-modules/tr-module-export/src/main/java/cn/tr/module/excel/core/handler/write/CustomCellWriteHandler.java

@@ -2,6 +2,7 @@ package cn.tr.module.excel.core.handler.write;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.*;
+import cn.tr.module.export.annotation.ExcelCellMerge;
 import cn.tr.module.export.annotation.ExcelSelect;
 import cn.tr.module.export.annotation.ExcelPropertySupport;
 import cn.tr.module.export.handler.AbstractCascadeSelectConverter;
@@ -15,6 +16,7 @@ import lombok.Data;
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddressList;
 import org.apache.poi.xssf.usermodel.*;
 
@@ -57,7 +59,41 @@ public class CustomCellWriteHandler implements CellWriteHandler {
             //进行日期格式校验
             createDateColumn(field,cell);
             columnIndexMap.put(field.getName(),cell.getColumnIndex());
+        }else {
+
+            //合并单元格
+
+        }
+    }
+
+    private void mergeCell(Field field,Cell cell){
+        ExcelPropertySupport excelPropertySupport = field.getAnnotation(ExcelPropertySupport.class);
+        if(excelPropertySupport==null||excelPropertySupport.merge()==null||!excelPropertySupport.merge().autoMerge()){
+            return;
         }
+        Sheet sheet = cell.getSheet();
+        ExcelCellMerge merge = excelPropertySupport.merge();
+        String cellValue = cell.getStringCellValue();
+        //不合并空值
+        if(StrUtil.isBlank(cellValue)&&!merge.mergeBlank()){
+            return;
+        }
+        int rowIndex = cell.getRowIndex();
+        //不与头部合并
+        if(rowIndex==2){
+            return;
+        }
+        Row row = sheet.getRow(rowIndex - 1);
+        Cell rowCell = row.getCell(cell.getColumnIndex());
+        String lastCellValue = rowCell.getStringCellValue();
+        if(StrUtil.isBlank(lastCellValue)&&!merge.mergeBlank()){
+            return;
+        }
+        if(StrUtil.equals(lastCellValue,cellValue)){
+            //单元格值相同,进行合并操作
+            sheet.addMergedRegion(new CellRangeAddress(rowIndex-1,rowIndex,cell.getColumnIndex(),cell.getColumnIndex()));
+        }
+
     }
 
     private void createDateColumn(Field field,Cell cell){