一般来说,实现文件的导出使用的是
Apache POI
以及Alibaba EasyExcel
我选择了第二个Alibaba EasyExcel
,主要是
- 导出的内容不需要随机指定
- 简单,上手快
一、SpringBoot
1. 导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
2. 编写实体类
实体类就是每一列的名字
@Data
public class TeamWithdrawMoneyDataVO {
/**
* 团队名
*/
@ExcelProperty(value = "团队名")
private String teamName;
/**
* 货币代码
*/
@ExcelProperty(value = "货币代码")
private String currencyCode;
/**
* 提币数量
*/
@ExcelProperty(value = "提币数量")
private BigDecimal number;
/**
* 提币地址
*/
@ExcelProperty(value = "提币地址")
private String address;
/**
* 备注
*/
@ExcelProperty(value = "备注")
private String remarks;
/**
* 提币状态: 0:审核中 1:审核通过 2: 审核未通过
*/
@ExcelProperty(value = "提币状态: 0:审核中 1:审核通过 2: 审核未通过")
private Integer state;
/**
* 手续费
*/
@ExcelProperty(value = "手续费")
private BigDecimal charge;
/**
* 创建时间
*/
@ExcelProperty(value = "创建时间")
private Date createTime;
/**
* 更新时间
*/
@ExcelProperty(value = "更新时间")
private Date updateTime;
}
3. 编写ExcelUtil
目前我集成了文件的导出,还可以集成文件的导入等等
等我有时间写完了就来补充
public class ExcelTool {
/**
* 默认excel文件名和单元sheet名一样的 Excel文件导出
* @param workBookName: excel文件名
* @param sheetName:工作簿
* @param list:导入表格的数据
* @param pojoClass :工作表映射实体类
* @param response :客户端输出数据流
* @return void
* @author hanyulin
* @date 2021/10/21 16:33
*/
public static void exportExcel(String workBookName, String sheetName, List<?> list, Class<?> pojoClass, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(workBookName + System.currentTimeMillis() + ".xlsx", "utf-8"));
EasyExcel.write(response.getOutputStream(), pojoClass).sheet(sheetName).doWrite(list);
} catch (IOException e) {
throw new YyException("下载报表异常");
}
}
}
4. 服务生产方:controller
需要注意的一点就是,返回值一定要是
void
@GetMapping("/download")
@ApiOperation("导出数据")
public void download(HttpServletResponse response) {
// 工作簿中插入的数据集合,根据实际情况进行调整
List<TeamWithdrawMoneyDataVO> list = teamWithdrawMoneyService.download();
ExcelTool.exportExcel("团队提币记录", "sheet01", list, TeamWithdrawMoneyDataVO.class, response);
}
SpringBoot使用EasyExcel到这里就完结了。
SpringCloud还有两个步骤进行处理。继续对 服务消费方 以及 Feign调用进行处理
注:swagger 测试会导致些问题,我直接用的是浏览器调用接口
二、SpringCloud
feign在调用相应流的时候,还是有点小坑的
- feign接口这里也是需要返回值:feign.Response !!!(一般来说,feign生产方的请求类型的返回值与feign调用的返回值一样)
- 服务调用方在接受了feign接口的response,还要以流的方式输到浏览器
5. 服务调用方:controller
调用方还要对feign的response进行流处理
@GetMapping("/download")
@ApiOperation("导出数据")
@Login
public void download(HttpServletResponse servletResponse) throws UnsupportedEncodingException {
Response response = teamWithdrawMoneyFeign.download();
servletResponse.setContentType("application/vnd.ms-excel; charset=utf-8" );
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("团队提币记录" + System.currentTimeMillis() + ".xlsx", "utf-8"));
Response.Body body = response.body();
InputStream inputStream;
OutputStream outputStream;
try {
inputStream = body.asInputStream();
outputStream = servletResponse.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
inputStream.close();
outputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
6. Feign调用
@GetMapping("/teamWithdrawMoney/download")
/*
* 错误示范:
* void download(HttpServletResponse response);
* feign.Response download(HttpServletResponse response);
* 报错:getOutputStream() has already been called for this response
*/
feign.Response download();
“ 前端请求到B服务,B服务使用feign去调用A服务是输出流,这个时候A服务就需要将输出流响应到feign接口中的response上,B服务再拿到feign接口的response,在B服务这里将response以流的方式输到浏览器;
feign在这里将相当于一个中间人先拿到输出流中转一下。”
—— Feign遇到的坑:调用生产方获取响应流。