Easy Excel动态组合导出
作者:互联网
提示:为了回应开发同学,特写此文,希望能帮助到大家,谢谢大家支持和信任
文章目录
前言
Easy Excel 真的很好用,今天就为大家介绍如何使用它做动态导出。
链接: Easy Excel官网入口.
一、上菜
1.添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.5</version>
<exclusions>
<exclusion>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
2.核心编码
@ApiOperation(httpMethod = "POST", value = "下载路线规划报表", notes = "下载路线规划报表", produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/download/route-report")
public void downloadRouteReport(@RequestBody @Valid RouteReportDto reportDto, HttpServletResponse response) throws IOException {
String sheetName = "路线规划报表";
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("路线规划报表", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.setHeader("filename", fileName + ".xlsx");
RouteReportDto dto = getFormatDate(reportDto);
// 查询数据
List<RouteReport> list = routeReportService.getRouteReportInfoExcel(dto);
List<RouteReportExcel> excelList = new ArrayList<>();
// 数据格式包装
statusExcel(excelList,list);
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream(), RouteReportExcel.class).autoCloseStream(Boolean.FALSE).sheet(sheetName)
.doWrite(excelList0);
} catch (Exception e) {
// 重置response
log.error("导出异常!!!",e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<>();
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
3.导出字段属性介绍
// RouteReportExcel
@ExcelProperty 在每个需要导出的字段上面加上导出时需要展示的名称即可。
直接整一个组合型,可以灵活使用,不用拘泥于一种格式
@Data
@ApiModel(value = "报表数据")
public class RouteReportExcel implements Serializable {
@ExcelProperty({"时间", "自然月"})
@ApiModelProperty(value = "年月")
private String planMonth;
@ExcelProperty({"组织", "行销公司"})
@ApiModelProperty(value = "公司")
private String company;
@ExcelProperty({"组织", "营业部"})
@ApiModelProperty(value = "营业部")
private String dept;
@ExcelProperty({"组织", "营业所"})
@ApiModelProperty(value = "营业所")
private String orgName;
@ExcelProperty({"组织", "业代类型"})
@ApiModelProperty(value = "业代类型")
private String salesTypeName;
@ExcelProperty({"组织", "业代人员"})
@ApiModelProperty(value = "业代人员")
private String empName;
@ExcelProperty({"规划进度", "方案完成"})
@ApiModelProperty(value = "方案完成")
private String planDownloadStatus;
@ExcelProperty({"规划进度", "方案上传SFT"})
@ApiModelProperty(value = "方案上传SFT")
private String planUploadStatus;
}
标签:String,Excel,ExcelProperty,导出,value,ApiModelProperty,private,Easy,response 来源: https://blog.csdn.net/weixin_42153503/article/details/122188439