抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

导出表报一直就是个头疼的事情,EasyPOI 是一个对POI的封装的方便易用的工具类,帮助开发中节省了大量的时间,使开发人员脱离苦海。今天对自己业务使用中的例子,简单入个门。

安装依赖

1
2
3
4
5
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>

视图对象

HF-2024-07-20-10-29-59

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.example.pojo.vo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
* 队伍信息表
*/
@ApiModel(description = "队伍信息表")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "example.team")
public class TeamExportVO implements Serializable {

/**
* 队伍识别码
*/
@TableField(value = "team_code")
@ApiModelProperty(value = "队伍识别码")
@Excel(name = "队伍识别码", width = 20, height = 20)
private String teamCode;

/**
* 组别: 1学生组, 2职工教师组
*/
@TableField(value = "group_type")
@ApiModelProperty(value = "组别: 1学生组, 2职工教师组")
@Excel(name = "组别", width = 20, height = 20)
private String groupType;

/**
* 是否参赛 0为参赛 1已参数
*/
@TableField(value = "is_participate")
@ApiModelProperty(value = "是否参赛 0为参赛 1已参数")
@Excel(name = "是否参赛", width = 20, height = 20)
private String isParticipate;

/**
* 参赛单位名称
*/
@TableField(value = "org_name")
@ApiModelProperty(value = "参赛单位名称")
@Excel(name = "参赛单位名称", width = 20, height = 20)
private String orgName;

/**
* 统一社会信用代码
*/
@TableField(value = "sc_code")
@ApiModelProperty(value = "统一社会信用代码")
@Excel(name = "统一社会信用代码", width = 20, height = 20)
private String sCCode;

/**
* 法人证书照片路径
*/
@TableField(value = "legal_certificate_path")
@ApiModelProperty(value = "法人证书照片路径")
@Excel(name = "法人证书照片路径", type = 2, width = 20, height = 20,imageType = 1)
private String legalCertificatePath;

/**
* 队伍名称
*/
@TableField(value = "team_name")
@ApiModelProperty(value = "队伍名称")
@Excel(name = "队伍名称", width = 20)
private String teamName;

/**
* 领队姓名
*/
@TableField(value = "leader_name")
@ApiModelProperty(value = "领队姓名")
@Excel(name = "领队姓名", width = 20)
private String leaderName;

/**
* 领队联系电话
*/
@TableField(value = "leader_phone")
@ApiModelProperty(value = "领队联系电话")
@Excel(name = "领队联系电话", width = 20)
private String leaderPhone;

/**
* 领队邮箱
*/
@TableField(value = "leader_email")
@ApiModelProperty(value = "领队邮箱")
@Excel(name = "领队邮箱", width = 20)
private String leaderEmail;

/**
* 领队身份证号码
*/
@TableField(value = "leader_id_number")
@ApiModelProperty(value = "领队身份证号码")
@Excel(name = "领队身份证号码", width = 20)
private String leaderIdNumber;

/**
* 领队院校/部门
*/
@TableField(value = "leader_institution_department")
@ApiModelProperty(value = "领队院校/部门")
@Excel(name = "领队院校/部门", width = 20)
private String leaderInstitutionDepartment;

/**
* 领队职务
*/
@TableField(value = "leader_position")
@ApiModelProperty(value = "领队职务")
@Excel(name = "领队职务", width = 20)
private String leaderPosition;

/**
* 教练/指导老师姓名
*/
@TableField(value = "coach_name")
@ApiModelProperty(value = "教练/指导老师姓名")
@Excel(name = "教练/指导老师姓名", width = 20)
private String coachName;

/**
* 教练/指导老师联系电话
*/
@TableField(value = "coach_phone")
@ApiModelProperty(value = "教练/指导老师联系电话")
@Excel(name = "教练/指导老师联系电话", width = 20)
private String coachPhone;

/**
* 教练/指导老师邮箱
*/
@TableField(value = "coach_email")
@ApiModelProperty(value = "教练/指导老师邮箱")
@Excel(name = "教练/指导老师邮箱", width = 20)
private String coachEmail;

/**
* 教练/指导老师身份证号码
*/
@TableField(value = "coach_id_number")
@ApiModelProperty(value = "教练/指导老师身份证号码")
@Excel(name = "教练/指导老师身份证号码", width = 20)
private String coachIdNumber;

/**
* 教练/指导老师院校
*/
@TableField(value = "coach_institution")
@ApiModelProperty(value = "教练/指导老师院校")
@Excel(name = "教练/指导老师院校", width = 20)
private String coachInstitution;

/**
* 教练/指导老师职务
*/
@TableField(value = "coach_position")
@ApiModelProperty(value = "教练/指导老师职务")
@Excel(name = "教练/指导老师职务", width = 20)
private String coachPosition;

/**
* 创建时间
*/
@TableField(value = "created_at")
@ApiModelProperty(value = "创建时间")
private Date createdAt;

/**
* 更新时间
*/
@TableField(value = "updated_at")
@ApiModelProperty(value = "更新时间")
private Date updatedAt;

@TableField(value = "del_flag")
@ApiModelProperty(value = "")
private Boolean delFlag;

/**
* 成员姓名
*/
@TableField(value = "member_name")
@ApiModelProperty(value = "成员姓名")
@Excel(name = "成员1姓名", width = 20)
private String memberName1;

/**
* 成员联系电话
*/
@TableField(value = "member_phone")
@ApiModelProperty(value = "成员联系电话")
@Excel(name = "成员1联系电话", width = 20)
private String memberPhone1;

/**
* 成员身份证号码
*/
@TableField(value = "member_id_number")
@ApiModelProperty(value = "成员身份证号码")
@Excel(name = "成员1身份证号码", width = 20)
private String memberIdNumber1;

/**
* 成员院校/部门
*/
@TableField(value = "member_institution_department")
@ApiModelProperty(value = "成员院校/部门")
@Excel(name = "成员1院校/部门", width = 20)
private String memberInstitutionDepartment1;

/**
* 成员专业/职务
*/
@TableField(value = "member_major_position")
@ApiModelProperty(value = "成员专业/职务")
@Excel(name = "成员1专业/职务", width = 20)
private String memberMajorPosition1;

/**
* 证明图片路径
*/
@TableField(value = "proof_image_path")
@ApiModelProperty(value = "证明图片路径")
@Excel(name = "成员1证明图片路径", type = 2, width = 20, height = 20,imageType = 1)
private String proofImagePath1;

@TableField(value = "member_name")
@ApiModelProperty(value = "成员姓名")
@Excel(name = "成员2姓名", width = 20)
private String memberName2;

/**
* 成员联系电话
*/
@TableField(value = "member_phone")
@ApiModelProperty(value = "成员联系电话")
@Excel(name = "成员2联系电话", width = 20)
private String memberPhone2;

/**
* 成员身份证号码
*/
@TableField(value = "member_id_number")
@ApiModelProperty(value = "成员身份证号码")
@Excel(name = "成员2身份证号码", width = 20)
private String memberIdNumber2;

/**
* 成员院校/部门
*/
@TableField(value = "member_institution_department")
@ApiModelProperty(value = "成员院校/部门")
@Excel(name = "成员2院校/部门", width = 20)
private String memberInstitutionDepartment2;

/**
* 成员专业/职务
*/
@TableField(value = "member_major_position")
@ApiModelProperty(value = "成员专业/职务")
@Excel(name = "成员2专业/职务", width = 20)
private String memberMajorPosition2;

/**
* 证明图片路径
*/
@TableField(value = "proof_image_path")
@ApiModelProperty(value = "证明图片路径")
@Excel(name = "成员2证明图片路径", type = 2, width = 20, height = 20,imageType = 1)
private String proofImagePath2;


@TableField(value = "member_name")
@ApiModelProperty(value = "成员姓名")
@Excel(name = "成员3姓名", width = 20)
private String memberName3;

/**
* 成员联系电话
*/
@TableField(value = "member_phone")
@ApiModelProperty(value = "成员联系电话")
@Excel(name = "成员3联系电话", width = 20)
private String memberPhone3;

/**
* 成员身份证号码
*/
@TableField(value = "member_id_number")
@ApiModelProperty(value = "成员身份证号码")
@Excel(name = "成员3身份证号码", width = 20)
private String memberIdNumber3;

/**
* 成员院校/部门
*/
@TableField(value = "member_institution_department")
@ApiModelProperty(value = "成员院校/部门")
@Excel(name = "成员3院校/部门", width = 20)
private String memberInstitutionDepartment3;

/**
* 成员专业/职务
*/
@TableField(value = "member_major_position")
@ApiModelProperty(value = "成员专业/职务")
@Excel(name = "成员3专业/职务", width = 20)
private String memberMajorPosition3;

/**
* 证明图片路径
*/
@TableField(value = "proof_image_path")
@ApiModelProperty(value = "证明图片路径")
@Excel(name = "成员3证明图片路径", type = 2, width = 20, height = 20,imageType = 1)
private String proofImagePath3;

private static final long serialVersionUID = 1L;
}

导出工具类

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.common.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelStyleType;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.example.common.handle.exception.FileException;
import com.example.pojo.vo.TeamExportVO;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
* @Author: byronlau
* @Date: 2024/07/19/9:23
* @Description: 基于自己业务封装的EasyPOI 工具类,只需传入数据对象
*/
public class ExcelUtil {

public static <T> void exportExcel(HttpServletResponse response, Map<String, List<T>> dataMap, String fileName) {
try {
// 设置响应输出的头类型
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()));
List<Map<String, Object>> sheetsList = getMaps(dataMap);
//创建excel文件的方法
Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
//通过response输出流直接输入给客户端
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
workbook.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
throw new FileException("文件导出失败!!!", 500);
}
}

/**
* 数据组装
*
* @param dataMap
* @return
*/
private static <T> List<Map<String, Object>> getMaps(Map<String, List<T>> dataMap) {
List<Map<String, Object>> sheetsList = new ArrayList<>();
Set<String> keySet = dataMap.keySet();
keySet.forEach(k -> {
ExportParams exportParams = new ExportParams();
exportParams.setStyle(ExcelStyleType.BORDER.getClazz());
exportParams.setSheetName(k);
Map<String, Object> map = new HashMap<>();
// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
map.put("title", exportParams);
// 模版导出对应得实体类型,即包含了List的对象
map.put("entity", TeamExportVO.class);
// sheet中要填充得数据
map.put("data", dataMap.get(k));
sheetsList.add(map);
});
return sheetsList;
}
}

测试

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public static final String picPath = "E:\\workspace\\springboot-pure\\file\\user\\pic\\";

@Test
public void exportExcel() throws IOException {
Map<String, List<TeamExportVO>> teamExportMap = teamServiceImpl.exportByExample(exportVO, response);
// 设置下载的Excel名称,以当前时间为文件后缀,
String fileName = "参赛团队信息表-" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
ExcelUtil.exportExcel(response, teamExportMap, fileName);
}

public static List<TeamExportVO> generateTestData(int numberOfRecords) {
List<TeamExportVO> vos = new ArrayList<TeamExportVO>();
Random random = new Random();
for (int i = 0; i < numberOfRecords; i++) {
TeamExportVO team = new TeamExportVO();

team.setTeamCode("TEAM-" + IdUtil.simpleUUID());
team.setGroupType(i % 2 != 0 ? "学生组" : "职工教师组");
team.setIsParticipate(i % 2 == 0 ? "已参赛" : "未参赛");
team.setOrgName("Organization " + (1 + random.nextInt(100)));
team.setSCCode("12345678901234567" + (1 + random.nextInt(8)));
team.setLegalCertificatePath(picPath + "e072dbf0e07c4a7ca08786c9b16b8576.png");
team.setTeamName("Team " + (1 + random.nextInt(100)));
team.setLeaderName("Leader " + (1 + random.nextInt(100)));
team.setLeaderPhone("+1234567890" + (1 + random.nextInt(9)));
team.setLeaderEmail("leader" + i + "@example.com");
team.setLeaderIdNumber("12345678901234567" + (1 + random.nextInt(8)));
team.setLeaderInstitutionDepartment("Department " + (1 + random.nextInt(100)));
team.setLeaderPosition("Position " + (1 + random.nextInt(100)));
team.setCoachName("Coach " + (1 + random.nextInt(100)));
team.setCoachPhone("+1234567890" + (1 + random.nextInt(9)));
team.setCoachEmail("coach" + i + "@example.com");
team.setCoachIdNumber("12345678901234567" + (1 + random.nextInt(8)));
team.setCoachInstitution("Institution " + (1 + random.nextInt(100)));
team.setCoachPosition("Position " + (1 + random.nextInt(100)));
team.setCreatedAt(new Date());
team.setUpdatedAt(new Date());
team.setDelFlag(false);

// Generate member data

String memberPrefix1 = "Member " + 1 + " ";
team.setMemberName1(memberPrefix1 + (1 + random.nextInt(100)));
team.setMemberPhone1("+1234567890" + (1 + random.nextInt(9)));
team.setMemberIdNumber1("12345678901234567" + (1 + random.nextInt(8)));
team.setMemberInstitutionDepartment1("Department " + (1 + random.nextInt(100)));
team.setMemberMajorPosition1("Major/Position " + (1 + random.nextInt(100)));
team.setProofImagePath1(picPath + "0c3c0af683cf43208c8bce35d394763d.png");

String memberPrefix2 = "Member " + 2 + " ";
team.setMemberName2(memberPrefix2 + (1 + random.nextInt(100)));
team.setMemberPhone2("+1234567890" + (1 + random.nextInt(9)));
team.setMemberIdNumber2("12345678901234567" + (1 + random.nextInt(8)));
team.setMemberInstitutionDepartment2("Department " + (1 + random.nextInt(100)));
team.setMemberMajorPosition2("Major/Position " + (1 + random.nextInt(100)));
team.setProofImagePath2(picPath + "0c3c0af683cf43208c8bce35d394763d.png");

String memberPrefix3 = "Member " + 3 + " ";
team.setMemberName3(memberPrefix3 + (1 + random.nextInt(100)));
team.setMemberPhone3("+1234567890" + (1 + random.nextInt(9)));
team.setMemberIdNumber3("12345678901234567" + (1 + random.nextInt(8)));
team.setMemberInstitutionDepartment3("Department " + (1 + random.nextInt(100)));
team.setMemberMajorPosition3("Major/Position " + (1 + random.nextInt(100)));
team.setProofImagePath3(picPath + "0c3c0af683cf43208c8bce35d394763d.png");

vos.add(team);
}
return vos;
}

附上一个网站比较全的工具类 EasyPoi使用记录 by johnny233

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

/**
* Excel工具类
*/
public class ExcelUtil {
/**
* 导出
*
* @param list 数据列表
* @param title 标题
* @param sheetName sheet名称
* @param pojoClass 元素类型
* @param fileName 文件名
* @param isCreateHeader 是否创建列头
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
this.defaultExport(list, pojoClass, fileName, response, exportParams);
}

/**
* 导出
*
* @param list 数据列表
* @param title 标题
* @param sheetName sheet名称
* @param pojoClass 元素类型
* @param fileName 文件名
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
this.defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}

/**
* 导出,适用于导出多个sheet的Excel,配合createOneSheet方法一起使用
*
* @param list 数据列表(元素是Map)
* @param fileName 文件名
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
this.defaultExport(list, fileName, response);
}

/**
* 导入
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return Collections.emptyList();
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}

public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {
if (file == null) {
return Collections.emptyList();
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}

/**
* 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
*
* @param file 导入文件
* @param sheetIndex 导入sheet索引,从0开始
* @param titleRows 表标题的行数
* @param headerRows 表头行数
* @param pojoClass Excel实体类
*/
public static <T> List<T> importExcel(MultipartFile file, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {
if (file == null) {
return Collections.emptyList();
}
// 根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
ImportParams params = new ImportParams();
// 第几个sheet页
params.setStartSheetIndex(sheetIndex);
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}

/**
* 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
*
* @param filePath 导入文件路径
* @param sheetIndex 导入sheet索引
* @param titleRows 表标题的行数
* @param headerRows 表头行数
* @param pojoClass Excel实体类
*/
public static <T> List<T> importExcel(String filePath, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
ImportParams params = new ImportParams();
params.setStartSheetIndex(sheetIndex);
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}

/**
* 用于导出多sheet的Excel文件
*
* @param sheetName 自定义sheetName
* @param clazz pojo实体类
* @param data List<Map<String, Object>> or List<POJO>
* @return map
*/
public static Map<String, Object> createOneSheet(String sheetName, Class<?> clazz, List<?> data) {
ExportParams params = new ExportParams("", sheetName, ExcelType.XSSF);
return this.createOneSheet(params, clazz, data);
}

/**
* 创建一个表格并填充内容,返回map供工作簿使用,map的key必须写死
*
* @param params 导出配置
* @param clazz 带@Excel注解字段的POJO实体类
* @param data List<Map<String, Object>> or List<POJO>
* @return map
*/
private static Map<String, Object> createOneSheet(ExportParams params, Class<?> clazz, List<?> data) {
Map<String, Object> map = new HashMap<>(8);
map.put("title", params);
map.put("entity", clazz);
map.put("data", data);
return map;
}

private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null) {
this.downLoadExcel(fileName, response, workbook);
}
}

private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
if (workbook != null) {
this.downLoadExcel(fileName, response, workbook);
}
}

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
}
}

参考文章

评论