Browse Source

后台导出excel优化

24282 1 year ago
parent
commit
f29f58ce5b

+ 43 - 19
sd-business/src/main/java/com/sd/business/strategy/impl/DocumentByOrderExcelExportStrategy.java

@@ -1,6 +1,8 @@
 package com.sd.business.strategy.impl;
 package com.sd.business.strategy.impl;
 
 
+import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.EasyExcel;
@@ -56,18 +58,33 @@ public class DocumentByOrderExcelExportStrategy implements ExcelExportStrategy<M
         List<ExportDocumentByOrderBo> exportDocumentByOrderBos = new ArrayList<>();
         List<ExportDocumentByOrderBo> exportDocumentByOrderBos = new ArrayList<>();
         for (DocumentByOrderVo documentByOrderVo : list) {
         for (DocumentByOrderVo documentByOrderVo : list) {
             List<DocumentByOrderVo.SkuSpec> skuSpecList = documentByOrderVo.getSkuSpecList();
             List<DocumentByOrderVo.SkuSpec> skuSpecList = documentByOrderVo.getSkuSpecList();
-            for (DocumentByOrderVo.SkuSpec skuSpec : skuSpecList) {
+            DocumentByOrderVo.SkuSpec skuSpec = skuSpecList.get(0);
+
+            for (int j = 0; j < skuSpecList.size(); j++) {
                 List<DocumentByOrderVo.BomSpec> bomSpecList = skuSpec.getBomSpecList();
                 List<DocumentByOrderVo.BomSpec> bomSpecList = skuSpec.getBomSpecList();
-                for (DocumentByOrderVo.BomSpec bomSpec : bomSpecList) {
-                    ExportDocumentByOrderBo exportDocumentByOrderBo = ExportDocumentByOrderBo.builder()
-                            .wlnCreateTime(documentByOrderVo.getWlnCreateTime())
-                            .code(documentByOrderVo.getCode())
-                            .wlnCode(documentByOrderVo.getWlnCode())
-                            .skuSpecCode(skuSpec.getSkuSpecCode())
-                            .skuSpecName(skuSpec.getSkuSpecName())
-                            .quantity(skuSpec.getQuantity())
-                            .unitPrice(skuSpec.getUnitPrice())
-                            .subtotal(skuSpec.getSubtotal())
+
+                for (int i = 0; i < bomSpecList.size(); i++) {
+                    DocumentByOrderVo.BomSpec bomSpec = bomSpecList.get(i);
+
+                    ExportDocumentByOrderBo.ExportDocumentByOrderBoBuilder builder = ExportDocumentByOrderBo.builder();
+
+                    if (i == 0) {
+                        builder.skuSpecCode(skuSpec.getSkuSpecCode())
+                                .skuSpecName(skuSpec.getSkuSpecName())
+                                .quantity(skuSpec.getQuantity())
+                                .unitPrice(skuSpec.getUnitPrice())
+                                .subtotal(skuSpec.getSubtotal());
+                    }
+
+                    if (j == 0) {
+                        builder.wlnCreateTime(documentByOrderVo.getWlnCreateTime())
+                                .code(documentByOrderVo.getCode())
+                                .wlnCode(documentByOrderVo.getWlnCode())
+                                .outerBoxPackingFee(documentByOrderVo.getOuterBoxPackingFee())
+                                .total(documentByOrderVo.getTotal());
+                    }
+
+                    ExportDocumentByOrderBo exportDocumentByOrderBo = builder
                             .bomSpecCode(bomSpec.getBomSpecCode())
                             .bomSpecCode(bomSpec.getBomSpecCode())
                             .bomSpecName(bomSpec.getBomSpecName())
                             .bomSpecName(bomSpec.getBomSpecName())
                             .bomQuantity(bomSpec.getQuantity())
                             .bomQuantity(bomSpec.getQuantity())
@@ -78,12 +95,13 @@ public class DocumentByOrderExcelExportStrategy implements ExcelExportStrategy<M
                             .deliveryMaterialsFeeSummary(bomSpec.getDeliveryMaterialsFeeSummary())
                             .deliveryMaterialsFeeSummary(bomSpec.getDeliveryMaterialsFeeSummary())
                             .packingLaborSummary(bomSpec.getPackingLaborSummary())
                             .packingLaborSummary(bomSpec.getPackingLaborSummary())
                             .managementFeeSummary(bomSpec.getManagementFeeSummary())
                             .managementFeeSummary(bomSpec.getManagementFeeSummary())
-                            .outerBoxPackingFee(documentByOrderVo.getOuterBoxPackingFee())
-                            .total(documentByOrderVo.getTotal())
                             .build();
                             .build();
                     exportDocumentByOrderBos.add(exportDocumentByOrderBo);
                     exportDocumentByOrderBos.add(exportDocumentByOrderBo);
+
                 }
                 }
+
             }
             }
+
             all = all.add(documentByOrderVo.getTotal());
             all = all.add(documentByOrderVo.getTotal());
         }
         }
 
 
@@ -106,25 +124,31 @@ public class DocumentByOrderExcelExportStrategy implements ExcelExportStrategy<M
             if (!tempExcel.exists()) {
             if (!tempExcel.exists()) {
                 tempExcel.mkdir();
                 tempExcel.mkdir();
             }
             }
-            FileOutputStream os = new FileOutputStream(filePath);
 
 
             ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + "orderDocument.xlsx");
             ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + "orderDocument.xlsx");
-            InputStream is = classPathResource.getInputStream();
-            ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(is).build();
+            InputStream is = null;
+            FileOutputStream os = null;
+            ExcelWriter excelWriter = null;
             try {
             try {
+                is = classPathResource.getInputStream();
+                os = new FileOutputStream(filePath);
+                excelWriter = EasyExcel.write(os).withTemplate(is).build();
                 DocumentByOrderExcelCellMergeStrategy strategy = new DocumentByOrderExcelCellMergeStrategy(list);
                 DocumentByOrderExcelCellMergeStrategy strategy = new DocumentByOrderExcelCellMergeStrategy(list);
                 WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(strategy).build();
                 WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(strategy).build();
                 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
                 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
 
 
                 excelWriter.fill(data, fillConfig, writeSheet);
                 excelWriter.fill(data, fillConfig, writeSheet);
-                excelWriter.fill(exportDocumentByOrderBos, fillConfig, writeSheet);
+                for (List<ExportDocumentByOrderBo> documentByOrderBos : ListUtil.partition(exportDocumentByOrderBos, 1000)) {
+                    excelWriter.fill(documentByOrderBos, fillConfig, writeSheet);
+                }
+
             } finally {
             } finally {
                 if (excelWriter != null) {
                 if (excelWriter != null) {
                     excelWriter.finish();
                     excelWriter.finish();
                     excelWriter.close();
                     excelWriter.close();
                 }
                 }
-                os.close();
-                is.close();
+                IoUtil.close(os);
+                IoUtil.close(is);
             }
             }
 
 
             File file = new File(filePath);
             File file = new File(filePath);

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

@@ -1,21 +1,11 @@
-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.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.exception.ServiceException;
 import com.sd.SdApplication;
 import com.sd.SdApplication;
 import com.sd.business.entity.order.po.OrderInfo;
 import com.sd.business.entity.order.po.OrderInfo;
 import com.sd.business.entity.production.dto.StockPreparationDto;
 import com.sd.business.entity.production.dto.StockPreparationDto;
 import com.sd.business.entity.production.vo.UncompletedVo;
 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.dto.GetDocumentDto;
 import com.sd.business.entity.statement.vo.DocumentByOrderVo;
 import com.sd.business.entity.statement.vo.DocumentByOrderVo;
 import com.sd.business.service.order.OrderService;
 import com.sd.business.service.order.OrderService;
@@ -23,23 +13,17 @@ import com.sd.business.service.production.StockPreparationService;
 import com.sd.business.service.statement.StatementOfAccountMergeService;
 import com.sd.business.service.statement.StatementOfAccountMergeService;
 import com.sd.wln.service.WlnStatementOfAccount;
 import com.sd.wln.service.WlnStatementOfAccount;
 import com.sd.wln.util.WlnUtil;
 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.Test;
 import org.junit.runner.RunWith;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.core.io.ClassPathResource;
 import org.springframework.test.context.junit4.SpringRunner;
 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.math.BigDecimal;
-import java.nio.charset.Charset;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 @RunWith(SpringRunner.class)
 @RunWith(SpringRunner.class)
@@ -155,137 +139,140 @@ public class TestList {
         System.out.println(JSON.toJSONString(collect));
         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) {
     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<ExportDocumentByOrderBo> ssss = ListUtil.partition(exportDocumentByOrderBos, 100).get(0);
-            excelWriter.fill(exportDocumentByOrderBos, fillConfig, writeSheet);
-            System.out.println("写入完成");
-        } finally {
-            if (excelWriter != null) {
-                excelWriter.finish();
-                excelWriter.close();
-            }
-            os.close();
-            is.close();
-        }
-
+        String a = "{\n" +
+                "    \"idGroupConcat\": \"1699739286011727873,1702638389020454914,1700101673936044034,1700464061822562305,1702276001603575809,1700826449696530434,1701928713001660417,1701551225520091137,1701106726288019458,1701106725822451713,1701106725755342850,1701106725625319426,1701106725558210565,1701106725558210564,1699376899223990273\"\n" +
+                "}";
+        System.out.println(a);
     }
     }
 
 
-    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);
-        }
-
-    }
+    // @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);
+    //     }
+    //
+    // }
 
 
 
 
 }
 }