24282 1 жил өмнө
parent
commit
b21863b52b

+ 25 - 19
sd-business/src/main/java/com/sd/business/service/statement/impl/DocumentByOrderExcelCellMergeStrategy.java

@@ -13,8 +13,8 @@ import java.util.List;
 
 public class DocumentByOrderExcelCellMergeStrategy extends AbstractMergeStrategy {
 
-    // 无需合并的行数
-    private final int mergeRowIndex = 2;
+    // 最大行数
+    private final int maxRow;
 
     // sku需要合并的列
     private final List<Integer> skuColIndex = Arrays.asList(3, 4, 5, 16, 17);
@@ -27,6 +27,12 @@ public class DocumentByOrderExcelCellMergeStrategy extends AbstractMergeStrategy
     private final List<Integer> orderRowIndex = new ArrayList<>();
 
     public DocumentByOrderExcelCellMergeStrategy(List<DocumentByOrderVo> documentByOrderVoList) {
+
+        // 无需合并的行数
+        int mergeRowIndex = 2;
+        skuRowIndex.add(mergeRowIndex);
+        orderRowIndex.add(mergeRowIndex);
+
         for (DocumentByOrderVo documentByOrderVo : documentByOrderVoList) {
             List<DocumentByOrderVo.SkuSpec> skuSpecList = documentByOrderVo.getSkuSpecList();
 
@@ -43,38 +49,38 @@ public class DocumentByOrderExcelCellMergeStrategy extends AbstractMergeStrategy
                 skuRowIndex.add(bomSpecListSize + skuRowIndexNumber);
             }
         }
+
+        maxRow = skuRowIndex.get(skuRowIndex.size() - 1);
     }
 
     @Override
     protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
 
         // 当前行列
-        int curRowIndex = cell.getRowIndex(), curColIndex = cell.getColumnIndex();
+        int curRowIndex = cell.getRowIndex();
 
-        // 前3行不合并
-        if (curRowIndex <= mergeRowIndex) {
+        // 判断是否是最后一行
+        if (curRowIndex != maxRow) {
             return;
         }
 
+        int curColIndex = cell.getColumnIndex();
+
         // 合并订单列
-        if (orderColIndex.contains(curColIndex) && orderRowIndex.contains(curRowIndex)) {
-            int i = orderRowIndex.indexOf(curRowIndex);
-            if (i == 0) {
-                mergeWithPrevRow(sheet, mergeRowIndex, curRowIndex, curColIndex);
-            } else {
-                Integer row = orderRowIndex.get(i - 1);
-                mergeWithPrevRow(sheet, row, curRowIndex, curColIndex);
+        if (orderColIndex.contains(curColIndex)) {
+            for (int i = 1; i < orderRowIndex.size(); i++) {
+                Integer beginRow = orderRowIndex.get(i - 1);
+                Integer endRow = orderRowIndex.get(i);
+                mergeWithPrevRow(sheet, beginRow, endRow, curColIndex);
             }
         }
 
         // 合并sku列
-        else if (skuColIndex.contains(curColIndex) && skuRowIndex.contains(curRowIndex)) {
-            int i = skuRowIndex.indexOf(curRowIndex);
-            if (i == 0) {
-                mergeWithPrevRow(sheet, mergeRowIndex, curRowIndex, curColIndex);
-            } else {
-                Integer row = skuRowIndex.get(i - 1);
-                mergeWithPrevRow(sheet, row, curRowIndex, curColIndex);
+        else if (skuColIndex.contains(curColIndex)) {
+            for (int i = 1; i < skuRowIndex.size(); i++) {
+                Integer beginRow = skuRowIndex.get(i - 1);
+                Integer endRow = skuRowIndex.get(i);
+                mergeWithPrevRow(sheet, beginRow, endRow, curColIndex);
             }
         }
 

+ 156 - 134
sd-starter/src/test/java/TestList.java

@@ -1,11 +1,21 @@
+import cn.hutool.core.collection.ListUtil;
+import cn.hutool.core.io.FileUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.write.merge.AbstractMergeStrategy;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.ruoyi.common.exception.ServiceException;
 import com.sd.SdApplication;
 import com.sd.business.entity.order.po.OrderInfo;
 import com.sd.business.entity.production.dto.StockPreparationDto;
 import com.sd.business.entity.production.vo.UncompletedVo;
+import com.sd.business.entity.statement.bo.ExportDocumentByOrderBo;
 import com.sd.business.entity.statement.dto.GetDocumentDto;
 import com.sd.business.entity.statement.vo.DocumentByOrderVo;
 import com.sd.business.service.order.OrderService;
@@ -13,17 +23,23 @@ import com.sd.business.service.production.StockPreparationService;
 import com.sd.business.service.statement.StatementOfAccountMergeService;
 import com.sd.wln.service.WlnStatementOfAccount;
 import com.sd.wln.util.WlnUtil;
+import lombok.SneakyThrows;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.nio.charset.Charset;
+import java.util.*;
 import java.util.stream.Collectors;
 
 @RunWith(SpringRunner.class)
@@ -139,140 +155,146 @@ public class TestList {
         System.out.println(JSON.toJSONString(collect));
     }
 
+    // public static void main(String[] args) {
+    //     String a = "{\n" +
+    //             "    \"idGroupConcat\": \"1699739286011727873,1702638389020454914,1700101673936044034,1700464061822562305,1702276001603575809,1700826449696530434,1701928713001660417,1701551225520091137,1701106726288019458,1701106725822451713,1701106725755342850,1701106725625319426,1701106725558210565,1701106725558210564,1699376899223990273\"\n" +
+    //             "}";
+    //     System.out.println(a);
+    // }
+
+
+    @SneakyThrows
     public static void main(String[] args) {
-        String a = "{\n" +
-                "    \"idGroupConcat\": \"1699739286011727873,1702638389020454914,1700101673936044034,1700464061822562305,1702276001603575809,1700826449696530434,1701928713001660417,1701551225520091137,1701106726288019458,1701106725822451713,1701106725755342850,1701106725625319426,1701106725558210565,1701106725558210564,1699376899223990273\"\n" +
-                "}";
-        System.out.println(a);
+
+        String str2 = FileUtil.readString("E:\\新建文本文档2.txt", Charset.defaultCharset());
+        List<DocumentByOrderVo> list = JSON.parseArray(str2).toJavaList(DocumentByOrderVo.class);
+
+        String str = FileUtil.readString("E:\\新建文本文档.txt", Charset.defaultCharset());
+
+        JSONObject json = JSON.parseObject(str);
+        List<ExportDocumentByOrderBo> exportDocumentByOrderBos = (List<ExportDocumentByOrderBo>) json.remove("exportDocumentByOrderBos");
+
+        String filePath = "tempExcel" + File.separator + IdWorker.getId() + ".xlsx";
+        File tempExcel = new File("tempExcel");
+        if (!tempExcel.exists()) {
+            tempExcel.mkdir();
+        }
+        FileOutputStream os = new FileOutputStream(filePath);
+
+        ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + "orderDocument.xlsx");
+        InputStream is = classPathResource.getInputStream();
+        ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(is).build();
+        try {
+            A strategy = new A(list);
+            WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(strategy).build();
+            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
+
+            System.out.println("写入1");
+            excelWriter.fill(json, fillConfig, writeSheet);
+            System.out.println("写入2");
+            List<List<ExportDocumentByOrderBo>> ssss = ListUtil.partition(exportDocumentByOrderBos, 10);
+            for (List<ExportDocumentByOrderBo> documentByOrderBos : ssss) {
+                excelWriter.fill(documentByOrderBos, fillConfig, writeSheet);
+            }
+
+            System.out.println("写入完成");
+        } finally {
+            if (excelWriter != null) {
+                excelWriter.finish();
+                excelWriter.close();
+            }
+            os.close();
+            is.close();
+        }
+
     }
 
+    private static class A extends AbstractMergeStrategy {
 
-    // @SneakyThrows
-    // public static void main(String[] args) {
-    //
-    //     String str2 = FileUtil.readString("E:\\新建文本文档2.txt", Charset.defaultCharset());
-    //     List<DocumentByOrderVo> list = JSON.parseArray(str2).toJavaList(DocumentByOrderVo.class);
-    //
-    //     String str = FileUtil.readString("E:\\新建文本文档.txt", Charset.defaultCharset());
-    //
-    //     JSONObject json = JSON.parseObject(str);
-    //     List<ExportDocumentByOrderBo> exportDocumentByOrderBos = (List<ExportDocumentByOrderBo>) json.remove("exportDocumentByOrderBos");
-    //
-    //     String filePath = "tempExcel" + File.separator + IdWorker.getId() + ".xlsx";
-    //     File tempExcel = new File("tempExcel");
-    //     if (!tempExcel.exists()) {
-    //         tempExcel.mkdir();
-    //     }
-    //     FileOutputStream os = new FileOutputStream(filePath);
-    //
-    //     ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + "orderDocument.xlsx");
-    //     InputStream is = classPathResource.getInputStream();
-    //     ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(is).build();
-    //     try {
-    //         A strategy = new A(list);
-    //         WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(strategy).build();
-    //         FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
-    //
-    //         System.out.println("写入1");
-    //         excelWriter.fill(json, fillConfig, writeSheet);
-    //         System.out.println("写入2");
-    //         List<List<ExportDocumentByOrderBo>> ssss = ListUtil.partition(exportDocumentByOrderBos, 10);
-    //         for (List<ExportDocumentByOrderBo> documentByOrderBos : ssss) {
-    //             excelWriter.fill(documentByOrderBos, fillConfig, writeSheet);
-    //         }
-    //
-    //         System.out.println("写入完成");
-    //     } finally {
-    //         if (excelWriter != null) {
-    //             excelWriter.finish();
-    //             excelWriter.close();
-    //         }
-    //         os.close();
-    //         is.close();
-    //     }
-    //
-    // }
-    //
-    // private static class A extends AbstractMergeStrategy {
-    //
-    //     // 无需合并的行数
-    //     private final int mergeRowIndex = 2;
-    //
-    //     // sku需要合并的列
-    //     private final List<Integer> skuColIndex = Arrays.asList(3, 4, 5, 16, 17, 18, 19);
-    //     // sku无需合并的行
-    //     private final List<Integer> skuRowIndex = new ArrayList<>();
-    //
-    //     // 订单无需合并的行
-    //     private final List<Integer> orderRowIndex = new ArrayList<>();
-    //     // 订单需要合并的列
-    //     private final List<Integer> orderColIndex = Arrays.asList(0, 1, 2, 20, 21);
-    //
-    //     public A(List<DocumentByOrderVo> documentByOrderVoList) {
-    //
-    //         for (DocumentByOrderVo documentByOrderVo : documentByOrderVoList) {
-    //             List<DocumentByOrderVo.SkuSpec> skuSpecList = documentByOrderVo.getSkuSpecList();
-    //
-    //             // 赋值订单无需合并的行
-    //             Integer size = skuSpecList.stream().map(item -> item.getBomSpecList().size()).reduce(0, Integer::sum);
-    //             int orderRowIndexNumber = orderRowIndex.size() == 0 ? mergeRowIndex : orderRowIndex.get(orderRowIndex.size() - 1);
-    //             orderRowIndex.add(size + orderRowIndexNumber);
-    //
-    //             // 赋值sku无需合并的行
-    //             for (DocumentByOrderVo.SkuSpec skuSpec : skuSpecList) {
-    //                 int bomSpecListSize = skuSpec.getBomSpecList().size();
-    //                 int skuRowIndexSize = skuRowIndex.size();
-    //                 int skuRowIndexNumber = skuRowIndexSize == 0 ? mergeRowIndex : skuRowIndex.get(skuRowIndexSize - 1);
-    //                 skuRowIndex.add(bomSpecListSize + skuRowIndexNumber);
-    //             }
-    //         }
-    //
-    //     }
-    //
-    //     @Override
-    //     protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
-    //
-    //         // 当前行列
-    //         int curRowIndex = cell.getRowIndex(), curColIndex = cell.getColumnIndex();
-    //
-    //         // 前3行不合并
-    //         if (curRowIndex <= mergeRowIndex) {
-    //             return;
-    //         }
-    //
-    //         // 合并订单列
-    //         if (orderColIndex.contains(curColIndex) && orderRowIndex.contains(curRowIndex)) {
-    //             int i = orderRowIndex.indexOf(curRowIndex);
-    //             if (i == 0) {
-    //                 mergeWithPrevRow(sheet, mergeRowIndex, curRowIndex, curColIndex);
-    //             } else {
-    //                 Integer row = orderRowIndex.get(i - 1);
-    //                 mergeWithPrevRow(sheet, row, curRowIndex, curColIndex);
-    //             }
-    //         }
-    //
-    //         // 合并sku列
-    //         else if (skuColIndex.contains(curColIndex) && skuRowIndex.contains(curRowIndex)) {
-    //             int i = skuRowIndex.indexOf(curRowIndex);
-    //             if (i == 0) {
-    //                 mergeWithPrevRow(sheet, mergeRowIndex, curRowIndex, curColIndex);
-    //             } else {
-    //                 Integer row = skuRowIndex.get(i - 1);
-    //                 mergeWithPrevRow(sheet, row, curRowIndex, curColIndex);
-    //             }
-    //         }
-    //
-    //     }
-    //
-    //     /**
-    //      * 合并单元格
-    //      */
-    //     private void mergeWithPrevRow(Sheet sheet, int beginRow, int endRow, int col) {
-    //         CellRangeAddress cellRangeAddress = new CellRangeAddress(beginRow + 1, endRow, col, col);
-    //         sheet.addMergedRegionUnsafe(cellRangeAddress);
-    //     }
-    //
-    // }
+        // 最大行数
+        private final int maxRow;
+
+        // sku需要合并的列
+        private final List<Integer> skuColIndex = Arrays.asList(3, 4, 5, 16, 17);
+        // sku无需合并的行
+        private final List<Integer> skuRowIndex = new ArrayList<>();
+
+        // 订单需要合并的列
+        private final List<Integer> orderColIndex = Arrays.asList(0, 1, 2, 18, 19);
+        // 订单无需合并的行
+        private final List<Integer> orderRowIndex = new ArrayList<>();
+
+
+        public A(List<DocumentByOrderVo> documentByOrderVoList) {
+
+            // 无需合并的行数
+            int mergeRowIndex = 2;
+            skuRowIndex.add(mergeRowIndex);
+            orderRowIndex.add(mergeRowIndex);
+
+            for (DocumentByOrderVo documentByOrderVo : documentByOrderVoList) {
+                List<DocumentByOrderVo.SkuSpec> skuSpecList = documentByOrderVo.getSkuSpecList();
+
+                // 赋值订单无需合并的行
+                Integer size = skuSpecList.stream().map(item -> item.getBomSpecList().size()).reduce(0, Integer::sum);
+                int orderRowIndexNumber = orderRowIndex.size() == 0 ? mergeRowIndex : orderRowIndex.get(orderRowIndex.size() - 1);
+                orderRowIndex.add(size + orderRowIndexNumber);
+
+                // 赋值sku无需合并的行
+                for (DocumentByOrderVo.SkuSpec skuSpec : skuSpecList) {
+                    int bomSpecListSize = skuSpec.getBomSpecList().size();
+                    int skuRowIndexSize = skuRowIndex.size();
+                    int skuRowIndexNumber = skuRowIndexSize == 0 ? mergeRowIndex : skuRowIndex.get(skuRowIndexSize - 1);
+                    skuRowIndex.add(bomSpecListSize + skuRowIndexNumber);
+                }
+            }
+
+            maxRow = skuRowIndex.get(skuRowIndex.size() - 1);
+
+            System.out.println();
+        }
+
+        @Override
+        protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
+
+            // 当前行列
+            int curRowIndex = cell.getRowIndex();
+
+            if (curRowIndex != maxRow) {
+                return;
+            }
+
+            int curColIndex = cell.getColumnIndex();
+
+            // 合并订单列
+            if (orderColIndex.contains(curColIndex)) {
+                for (int i = 1; i < orderRowIndex.size(); i++) {
+                    Integer beginRow = orderRowIndex.get(i - 1);
+                    Integer endRow = orderRowIndex.get(i);
+                    mergeWithPrevRow(sheet, beginRow, endRow, curColIndex);
+                }
+            }
+
+            // 合并sku列
+            else if (skuColIndex.contains(curColIndex)) {
+                for (int i = 1; i < skuRowIndex.size(); i++) {
+                    Integer beginRow = skuRowIndex.get(i - 1);
+                    Integer endRow = skuRowIndex.get(i);
+                    mergeWithPrevRow(sheet, beginRow, endRow, curColIndex);
+                }
+            }
+
+        }
+
+        /**
+         * 合并单元格
+         */
+        private void mergeWithPrevRow(Sheet sheet, int beginRow, int endRow, int col) {
+            CellRangeAddress cellRangeAddress = new CellRangeAddress(beginRow + 1, endRow, col, col);
+            sheet.addMergedRegionUnsafe(cellRangeAddress);
+        }
+
+    }
 
 
 }