Bladeren bron

add export data

Your Name 2 jaren geleden
bovenliggende
commit
81c533b6f1

+ 38
- 0
src/main/java/com/yunzhi/inte/common/ExcelUtils.java Bestand weergeven

@@ -1,6 +1,14 @@
1 1
 package com.yunzhi.inte.common;
2 2
 
3
+import cn.hutool.poi.excel.ExcelUtil;
3 4
 import com.alibaba.excel.EasyExcel;
5
+import com.alibaba.excel.ExcelWriter;
6
+import com.alibaba.excel.support.ExcelTypeEnum;
7
+import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
8
+import com.alibaba.excel.write.handler.WriteHandler;
9
+import com.alibaba.excel.write.merge.AbstractMergeStrategy;
10
+import com.alibaba.excel.write.metadata.WriteSheet;
11
+import com.yunzhi.inte.common.excel.CustomMergeStrategy;
4 12
 
5 13
 import javax.servlet.http.HttpServletResponse;
6 14
 import java.io.IOException;
@@ -24,4 +32,34 @@ public class ExcelUtils {
24 32
 
25 33
         EasyExcel.write(response.getOutputStream(), dataClass).sheet("sheet1").doWrite(data);
26 34
     }
35
+
36
+    /**
37
+     * 导出, 支持复杂一点的自定义
38
+     * @param response
39
+     * @param dataClass
40
+     * @param data
41
+     * @param fileName
42
+     * @param writeHandlerList
43
+     * @throws IOException
44
+     */
45
+    public static void flush2(HttpServletResponse response, Class dataClass, List data, String fileName, List<WriteHandler> writeHandlerList) throws IOException {
46
+        response.setContentType("application/vnd.ms-excel");
47
+        response.setCharacterEncoding("utf-8");
48
+        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
49
+        response.setHeader("Content-Disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
50
+
51
+        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).excelType(ExcelTypeEnum.XLSX).build();
52
+        ExcelWriterSheetBuilder writerSheetBuilder = EasyExcel.writerSheet("sheet1").head(dataClass);
53
+
54
+        if (null != writeHandlerList && writeHandlerList.size() > 0) {
55
+            for (WriteHandler writeHandler : writeHandlerList) {
56
+                writerSheetBuilder.registerWriteHandler(writeHandler);
57
+            }
58
+        }
59
+
60
+        WriteSheet writeSheet = writerSheetBuilder.build();
61
+
62
+        excelWriter.write(data, writeSheet);
63
+        excelWriter.finish();
64
+    }
27 65
 }

+ 81
- 0
src/main/java/com/yunzhi/inte/common/excel/CustomMergeStrategy.java Bestand weergeven

@@ -0,0 +1,81 @@
1
+package com.yunzhi.inte.common.excel;
2
+
3
+import com.alibaba.excel.metadata.Head;
4
+import com.alibaba.excel.util.CollectionUtils;
5
+import com.alibaba.excel.write.merge.AbstractMergeStrategy;
6
+import org.apache.poi.ss.usermodel.Cell;
7
+import org.apache.poi.ss.usermodel.Sheet;
8
+import org.apache.poi.ss.util.CellRangeAddress;
9
+
10
+import java.util.ArrayList;
11
+import java.util.List;
12
+
13
+public class CustomMergeStrategy extends AbstractMergeStrategy {
14
+
15
+    /**
16
+     * 分组,每几行合并一次
17
+     */
18
+    private List<Integer> exportFieldGroupCountList;
19
+
20
+    /**
21
+     * 目标合并列index
22
+     */
23
+    private Integer targetColumnIndex;
24
+
25
+    // 需要开始合并单元格的首行index
26
+    private Integer rowIndex;
27
+
28
+    // exportDataList为待合并目标列的值
29
+    public CustomMergeStrategy(List<String> exportDataList, Integer targetColumnIndex) {
30
+        this.exportFieldGroupCountList = getGroupCountList(exportDataList);
31
+        this.targetColumnIndex = targetColumnIndex;
32
+    }
33
+
34
+
35
+    @Override
36
+    protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) {
37
+        if (null == rowIndex) {
38
+            rowIndex = cell.getRowIndex();
39
+        }
40
+        // 仅从首行以及目标列的单元格开始合并,忽略其他
41
+        if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == targetColumnIndex) {
42
+            mergeGroupColumn(sheet);
43
+        }
44
+    }
45
+
46
+    private void mergeGroupColumn(Sheet sheet) {
47
+        int rowCount = rowIndex;
48
+        for (Integer count : exportFieldGroupCountList) {
49
+            if(count == 1) {
50
+                rowCount += count;
51
+                continue ;
52
+            }
53
+            // 合并单元格
54
+            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1, targetColumnIndex, targetColumnIndex);
55
+            sheet.addMergedRegionUnsafe(cellRangeAddress);
56
+            rowCount += count;
57
+        }
58
+    }
59
+
60
+    // 该方法将目标列根据值是否相同连续可合并,存储可合并的行数
61
+    private List<Integer> getGroupCountList(List<String> exportDataList){
62
+        if (CollectionUtils.isEmpty(exportDataList)) {
63
+            return new ArrayList<>();
64
+        }
65
+
66
+        List<Integer> groupCountList = new ArrayList<>();
67
+        int count = 1;
68
+
69
+        for (int i = 1; i < exportDataList.size(); i++) {
70
+            if (exportDataList.get(i).equals(exportDataList.get(i - 1))) {
71
+                count++;
72
+            } else {
73
+                groupCountList.add(count);
74
+                count = 1;
75
+            }
76
+        }
77
+        // 处理完最后一条后
78
+        groupCountList.add(count);
79
+        return groupCountList;
80
+    }
81
+}

+ 41
- 7
src/main/java/com/yunzhi/inte/controller/PackageController.java Bestand weergeven

@@ -1,17 +1,21 @@
1 1
 package com.yunzhi.inte.controller;
2 2
 
3
+import com.alibaba.excel.write.handler.WriteHandler;
4
+import com.alibaba.excel.write.merge.AbstractMergeStrategy;
3 5
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 6
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 7
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
-import com.yunzhi.inte.common.BaseController;
7
-import com.yunzhi.inte.common.Constants;
8
-import com.yunzhi.inte.common.ResponseBean;
8
+import com.yunzhi.inte.common.*;
9
+
10
+import java.util.ArrayList;
9 11
 import java.util.List;
12
+import java.util.stream.Collectors;
10 13
 
11
-import com.yunzhi.inte.common.StringUtils;
14
+import com.yunzhi.inte.common.excel.CustomMergeStrategy;
12 15
 import com.yunzhi.inte.entity.Dishes;
13 16
 import com.yunzhi.inte.entity.PackageDetail;
14 17
 import com.yunzhi.inte.entity.Store;
18
+import com.yunzhi.inte.vo.PackageExport;
15 19
 import io.swagger.annotations.Api;
16 20
 import io.swagger.annotations.ApiOperation;
17 21
 import io.swagger.annotations.ApiParam;
@@ -20,6 +24,8 @@ import org.springframework.web.bind.annotation.*;
20 24
 import com.yunzhi.inte.entity.Package;
21 25
 import com.yunzhi.inte.service.PackageService;
22 26
 
27
+import javax.servlet.http.HttpServletResponse;
28
+
23 29
 /**
24 30
  * 套餐;(package)表控制层
25 31
  * @author : http://njyunzhi.com
@@ -75,11 +81,39 @@ public class PackageController extends BaseController {
75 81
      * @return 查询结果
76 82
      */
77 83
     @ApiOperation("导出套餐")
78
-    @GetMapping("/export")
79
-    public ResponseBean export() throws Exception {
84
+    @GetMapping("/package/export")
85
+    public void export(HttpServletResponse response) throws Exception {
80 86
         List<PackageDetail> result = packageService.exportAll();
87
+        List<PackageExport> exportList = new ArrayList<>();
88
+        if (null != result && result.size() > 0) {
89
+            int no = 0;
90
+            String lastName = "";
91
+            for (int i = 0; i < result.size(); i += 1) {
92
+                PackageDetail detail = result.get(i);
93
+                if (!lastName.equals(detail.getPackageName())) {
94
+                    no += 1;
95
+                }
96
+                lastName = detail.getPackageName();
97
+
98
+                PackageExport item = new PackageExport();
99
+                item.setNo(no);
100
+                item.setPackageName(detail.getPackageName());
101
+                item.setDishName(String.format("【%s】%s", 1 == detail.getDishKind() ? "主食": "菜肴", detail.getDishName()));
102
+                exportList.add(item);
103
+            }
104
+        }
81 105
 
82
-        return ResponseBean.success(result);
106
+        // 单元格合并策略
107
+        List<String> mergeNo = exportList.stream().map(x -> x.getNo().toString()).collect(Collectors.toList());
108
+        List<String> mergeName = exportList.stream().map(PackageExport::getPackageName).collect(Collectors.toList());
109
+        CustomMergeStrategy mergeStrategy1 = new CustomMergeStrategy(mergeNo, 0);
110
+        CustomMergeStrategy mergeStrategy2 = new CustomMergeStrategy(mergeName, 1);
111
+        List<WriteHandler> mergeStrategyList = new ArrayList<WriteHandler>() {{
112
+            add(mergeStrategy1);
113
+            add(mergeStrategy2);
114
+        }};
115
+
116
+        ExcelUtils.flush2(response, PackageExport.class, exportList, "套餐列表", mergeStrategyList);
83 117
     }
84 118
 
85 119
     /**

+ 2
- 2
src/main/java/com/yunzhi/inte/entity/PackageDetail.java Bestand weergeven

@@ -34,7 +34,7 @@ public class PackageDetail implements Serializable,Cloneable{
34 34
     /** 套餐名称 */
35 35
     @ApiModelProperty(name = "套餐名称",notes = "")
36 36
     @TableField(exist = false)
37
-    private Integer packageName ;
37
+    private String packageName ;
38 38
     /** 菜肴ID */
39 39
     @ApiModelProperty(name = "菜肴ID",notes = "")
40 40
     private Integer dishId ;
@@ -42,7 +42,7 @@ public class PackageDetail implements Serializable,Cloneable{
42 42
     /** 菜肴名称 */
43 43
     @ApiModelProperty(name = "菜肴名称",notes = "")
44 44
     @TableField(exist = false)
45
-    private Integer dishName ;
45
+    private String dishName ;
46 46
     /** 菜肴类型 */
47 47
     @ApiModelProperty(name = "菜肴类型",notes = "1主食,2菜肴")
48 48
     private Integer dishKind ;

+ 14
- 1
src/main/java/com/yunzhi/inte/vo/PackageExport.java Bestand weergeven

@@ -1,6 +1,8 @@
1 1
 package com.yunzhi.inte.vo;
2 2
 
3 3
 
4
+import com.alibaba.excel.annotation.ExcelProperty;
5
+import com.alibaba.excel.annotation.write.style.ColumnWidth;
4 6
 import io.swagger.annotations.ApiModel;
5 7
 import io.swagger.annotations.ApiModelProperty;
6 8
 import lombok.Data;
@@ -9,6 +11,17 @@ import lombok.Data;
9 11
 @ApiModel("套餐导出")
10 12
 public class PackageExport {
11 13
 
12
-    @ApiModelProperty("套餐名称")
14
+    @ExcelProperty(value = "序号", index = 0)
15
+    @ApiModelProperty("序号")
16
+    Integer no;
17
+
18
+    @ColumnWidth(36)
19
+    @ExcelProperty(value = "套餐", index = 1)
20
+    @ApiModelProperty("套餐")
13 21
     String packageName;
22
+
23
+    @ColumnWidth(36)
24
+    @ExcelProperty(value = "菜肴", index = 2)
25
+    @ApiModelProperty("菜肴")
26
+    String dishName;
14 27
 }

+ 1
- 1
src/main/resources/mapper/PackageMapper.xml Bestand weergeven

@@ -15,6 +15,6 @@
15 15
         WHERE
16 16
             t.`status` = 1
17 17
         ORDER BY
18
-            t.`name` ASC
18
+            t.`name` ASC, s.dish_kind ASC
19 19
     </select>
20 20
 </mapper>