ExcelController.java
4.3 KB
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
package com.skua.modules.excel.controller;
import cn.hutool.json.JSONUtil;
import com.skua.core.api.vo.Result;
import com.skua.core.context.SpringContextUtils;
import com.skua.modules.excel.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
/**
* <pre>
* Excel导入/导出/模板
* </pre>
*
* @author sonin
* @version 1.0 2022/4/27 9:59
*/
@Slf4j
@RestController
@RequestMapping("/file/excel")
public class ExcelController {
@PostMapping("/import")
public void importCtrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
Result result = null;
try {
if (StringUtils.isEmpty(request.getParameter("component"))) {
result = Result.error("请输入组件component参数");
} else {
ExcelService excelService = (ExcelService) SpringContextUtils.getBean(request.getParameter("component"));
result = excelService.importHandle(request);
}
} catch (Exception e) {
e.printStackTrace();
log.error("import Excel error: {}", e.getMessage());
result = Result.error(e.getMessage());
} finally {
outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
}
}
@GetMapping("/export")
public void exportCtrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
Result result;
try {
if (StringUtils.isEmpty(request.getParameter("component"))) {
result = Result.error("请输入组件component参数");
outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
} else {
ExcelService excelService = (ExcelService) SpringContextUtils.getBean(request.getParameter("component"));
Workbook workbook = excelService.exportHandle(request);
workbook.write(outputStream);
}
} catch (Exception e) {
e.printStackTrace();
log.error("export Excel error: {}", e.getMessage());
result = Result.error(e.getMessage());
outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
} finally {
outputStream.flush();
outputStream.close();
}
}
@GetMapping("/template")
public void templateCtrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
Result result;
try {
if (StringUtils.isEmpty(request.getParameter("component"))) {
result = Result.error("请输入组件component参数");
outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
} else {
ExcelService excelService = (ExcelService) SpringContextUtils.getBean(request.getParameter("component"));
Workbook workbook = excelService.templateHandle(request);
workbook.write(outputStream);
}
} catch (Exception e) {
e.printStackTrace();
log.error("template Excel error: {}", e.getMessage());
result = Result.error(e.getMessage());
outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
} finally {
outputStream.flush();
outputStream.close();
}
}
}