JAVA快速导出Excel
easypoi可以通过注解的方式快速导出实体Excel。
# 导入easypoi依赖:
compile group: 'cn.afterturn', name: 'easypoi-base', version: '4.2.0'
compile group: 'cn.afterturn', name: 'easypoi-annotation', version: '4.2.0'
compile group: 'cn.afterturn', name: 'easypoi-web', version: '4.2.0'
1
2
3
4
2
3
4
# 导入工具类
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
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.List;
import java.util.Map;
/**
* @Classname ExcelUtil
* @Description Excel工具类
*/
@Component
public class ExcelUtil {
/**
* 导出excel
*
* @param list 泛型数据
* @param title 标题
* @param sheetName sheet的名称
* @param pojoClass 需要导出的对象
* @param fileName 文件名称
* @param isCreateHeader 是否创建表头
* @throws IOException void
*/
public void exportExcel(List<?> list, Class<?> pojoClass, String title, String sheetName, String fileName,
boolean isCreateHeader, HttpServletResponse response) throws IOException {
final ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
baseExport(list, pojoClass, fileName, exportParams, response);
}
/**
* 最基础的对象导出
*
* @param list 数据列表
* @param pojoClass 导出对象
* @param fileName 文件名称
* @param exportParams 导出文件属性
* @throws IOException void
*/
private void baseExport(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response)
throws IOException {
final Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, workbook, response);
}
/**
* 最基础的多sheet导出
*
* @param list 多个不同数据对象的列表
* @param fileName 文件名称
* @throws IOException void
*/
private void baseExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
final Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, workbook, response);
}
/**
* 文件下载
*
* @param fileName 文件名称
* @param workbook exce对象
* @throws IOException void
*/
private void downLoadExcel(String fileName, Workbook workbook, HttpServletResponse response) throws IOException {
ServletOutputStream output = null;
try {
final String downloadName = URLEncoder.encode(fileName + "." + "xlsx", StandardCharsets.UTF_8);
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + downloadName);
output = response.getOutputStream();
workbook.write(output);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (output != null) {
output.flush();
output.close();
}
}
}
}
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
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
# 在需要导出的实体上注解@Excel(name = "表头名"),如果需要指定长度可以指定width
@Excel(name = "姓名")
private String name;
1
2
2
# 使用
@Autowired
ExcelUtil excelUtil;
@GetMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) {
List<User> all;
String name = "用户列表";
try {
excelUtil.exportExcel(all, User.class, name, name, name, true, response);
} catch (IOException e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
上次更新: 10/23/2024