文章目录
前言
产品今天提了个需求,大概是这样的,来,请看大屏幕。。。额。。。搞错了,重来!来,请看需求
设计到多sheet、列表、图片的模板导出,先看成品
一、EasyExcel是什么?
EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
详细介绍见官网:EasyExcel官网
二、模板样式调整
三、使用步骤
1.引入jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
2.方法示例
2.1 Controller:
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
/**
1. <p>
2. 可靠性测试委托单 前端控制器
3. </p>
4. 5. @author zs
6. @since 2024-06-21
*/
@RestController
@Api(tags = "可靠性测试委托单")
@Validated
@Slf4j
@RequestMapping("/reliabitestEntrust")
public class ReliabitestEntrustController {
@Resource
private IReliabitestEntrustService entrustService;
@ApiOperation(value = "可靠性测试委托单-下载报告")
@GetMapping("/downEntrustReport")
public void downEntrustReport(HttpServletResponse response, @RequestParam("id") @NotNull(message = "id不能为空") Long id) {
try {
ByteArrayOutputStream oos = new ByteArrayOutputStream();
String fileName = entrustService.downEntrustReport(oos, id);
response.setHeader("Content-Disposition", "attachment;filename*=" + URLEncoder.encode(fileName, "UTF-8"));
oos.writeTo(response.getOutputStream());
oos.flush();
oos.close();
} catch (Exception e) {
log.error("可靠性测试委托单-下载报告出错了:{}", e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
try {
ResponseBean resp = ResponseBean.buildFail(CodeEnums.FAIL.getCode(), e.getMessage());
response.getWriter().println(JSONObject.toJSONString(resp));
} catch (Exception e1) {
log.error("可靠性测试委托单-下载报告,出错了:{}", e1);
}
}
}
}
2.2 Service:
/**
* <p>
* 可靠性测试委托单 服务类
* </p>
*
* @author zs
* @since 2024-06-21
*/
public interface IReliabitestEntrustService extends IService<ReliabitestEntrust> {
/**
* 功能: 下载委托单报告
* 调用时机: 点击下载报告按钮
* 注意事项: 参数必传
*
* @return
* @author zs
* @date 2024/6/27
*/
String downEntrustReport(OutputStream outputStream, Long id) throws Exception;
}
2.3 ServiceImpl:
/**
* 功能: 下载委托单报告
* 调用时机: 点击下载报告按钮
* 注意事项: 参数必传
*
* @return
* @author zs
* @date 2024/6/27
*/
@Override
public String downEntrustReport(OutputStream outputStream, Long id) throws Exception {
// Step 1: 获取委托单主表、详情、问题点与改善、委托单配置
CompletableFuture<ReliabitestEntrust> entrustFuture = CompletableFuture.supplyAsync(() -> this.getById(id));
CompletableFuture<List<ReliabitestEntrustDetail>> detailFuture = CompletableFuture.supplyAsync(() -> entrustDetailService.queryByEntrustId(id));
CompletableFuture<List<ReliabitestEntrustQuestion>> questionFuture = CompletableFuture.supplyAsync(() -> entrustQuestionService.queryByEntrustId(id));
// 获取config配置
ResponseBean<List<GetConfigListByTypeResp>> response = basicsClient.getConfigsByType(47);
CheckException.checkThrowException(() -> !response.isSuccess() || CollectionUtil.isEmpty(response.getData()), "委托单配置为空,请检查配置!!!");
List<GetConfigListByTypeResp> configList = response.getData();
// 试验项目 code+名称
Map<String, String> codeAndNameMap = configList.stream().collect(Collectors.toMap(e -> e.getCode(), a -> a.getName(), (k1, k2) -> k1));
// 子集
List<GetConfigListByTypeResp> childrenList = configList.stream().map(GetConfigListByTypeResp::getChildrenList)
.flatMap(List::stream).collect(Collectors.toList());
Map<String, String> childMap = childrenList.stream().collect(Collectors.toMap(e -> e.getCode(), a -> a.getName(), (k1, k2) -> k1));
// Step 2: 组装模板数据
ReliabitestEntrust entrust = entrustFuture.get();
// 主信息
DownEntrustReportDTO dto = new DownEntrustReportDTO();
if (ObjectUtil.isNotNull(entrust))