提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.demo.controller; |
X |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.dl.common.utils.poi.ExcelUtil; |
|
5 |
import lombok.AllArgsConstructor; |
|
6 |
import lombok.Data; |
|
7 |
import org.springframework.web.bind.annotation.GetMapping; |
|
8 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
9 |
import org.springframework.web.bind.annotation.RestController; |
|
10 |
|
|
11 |
import javax.servlet.http.HttpServletResponse; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.HashMap; |
|
14 |
import java.util.List; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
/** |
|
18 |
* 测试Excel功能 |
|
19 |
* |
|
20 |
* @author Lion Li |
|
21 |
*/ |
|
22 |
@RestController |
|
23 |
@RequestMapping("/demo/excel") |
|
24 |
public class TestExcelController { |
|
25 |
|
|
26 |
/** |
|
27 |
* 单列表多数据 |
|
28 |
*/ |
|
29 |
@GetMapping("/exportTemplateOne") |
|
30 |
public void exportTemplateOne(HttpServletResponse response) { |
|
31 |
Map<String, String> map = new HashMap<>(); |
|
32 |
map.put("title", "单列表多数据"); |
|
33 |
map.put("test1", "数据测试1"); |
|
34 |
map.put("test2", "数据测试2"); |
|
35 |
map.put("test3", "数据测试3"); |
|
36 |
map.put("test4", "数据测试4"); |
|
37 |
map.put("testTest", "666"); |
|
38 |
List<TestObj> list = new ArrayList<>(); |
|
39 |
list.add(new TestObj("单列表测试1", "列表测试1", "列表测试2", "列表测试3", "列表测试4")); |
|
40 |
list.add(new TestObj("单列表测试2", "列表测试5", "列表测试6", "列表测试7", "列表测试8")); |
|
41 |
list.add(new TestObj("单列表测试3", "列表测试9", "列表测试10", "列表测试11", "列表测试12")); |
|
42 |
ExcelUtil.exportTemplate(CollUtil.newArrayList(map, list), "单列表.xlsx", "excel/单列表.xlsx", response); |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 多列表多数据 |
|
47 |
*/ |
|
48 |
@GetMapping("/exportTemplateMuliti") |
|
49 |
public void exportTemplateMuliti(HttpServletResponse response) { |
|
50 |
Map<String, String> map = new HashMap<>(); |
|
51 |
map.put("title1", "标题1"); |
|
52 |
map.put("title2", "标题2"); |
|
53 |
map.put("title3", "标题3"); |
|
54 |
map.put("title4", "标题4"); |
|
55 |
map.put("author", "Lion Li"); |
|
56 |
List<TestObj1> list1 = new ArrayList<>(); |
|
57 |
list1.add(new TestObj1("list1测试1", "list1测试2", "list1测试3")); |
|
58 |
list1.add(new TestObj1("list1测试4", "list1测试5", "list1测试6")); |
|
59 |
list1.add(new TestObj1("list1测试7", "list1测试8", "list1测试9")); |
|
60 |
List<TestObj1> list2 = new ArrayList<>(); |
|
61 |
list2.add(new TestObj1("list2测试1", "list2测试2", "list2测试3")); |
|
62 |
list2.add(new TestObj1("list2测试4", "list2测试5", "list2测试6")); |
|
63 |
List<TestObj1> list3 = new ArrayList<>(); |
|
64 |
list3.add(new TestObj1("list3测试1", "list3测试2", "list3测试3")); |
|
65 |
List<TestObj1> list4 = new ArrayList<>(); |
|
66 |
list4.add(new TestObj1("list4测试1", "list4测试2", "list4测试3")); |
|
67 |
list4.add(new TestObj1("list4测试4", "list4测试5", "list4测试6")); |
|
68 |
list4.add(new TestObj1("list4测试7", "list4测试8", "list4测试9")); |
|
69 |
list4.add(new TestObj1("list4测试10", "list4测试11", "list4测试12")); |
|
70 |
Map<String, Object> multiListMap = new HashMap<>(); |
|
71 |
multiListMap.put("map", map); |
|
72 |
multiListMap.put("data1", list1); |
|
73 |
multiListMap.put("data2", list2); |
|
74 |
multiListMap.put("data3", list3); |
|
75 |
multiListMap.put("data4", list4); |
|
76 |
ExcelUtil.exportTemplateMultiList(multiListMap, "多列表.xlsx", "excel/多列表.xlsx", response); |
|
77 |
} |
|
78 |
|
|
79 |
@Data |
|
80 |
@AllArgsConstructor |
|
81 |
static class TestObj1 { |
|
82 |
private String test1; |
|
83 |
private String test2; |
|
84 |
private String test3; |
|
85 |
} |
|
86 |
|
|
87 |
@Data |
|
88 |
@AllArgsConstructor |
|
89 |
static class TestObj { |
|
90 |
private String name; |
|
91 |
private String list1; |
|
92 |
private String list2; |
|
93 |
private String list3; |
|
94 |
private String list4; |
|
95 |
} |
|
96 |
|
|
97 |
} |