使用easyExcel导出简单文档操作
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
Controller
入参本来是实体对象的,后来前端把对象转换成jsonStr了
@GetMapping("/export")
public void export(HttpServletResponse resp, @RequestParam String jsonStr){
service.export(resp ,jsonStr);
}
// jsonStr OrderDetailReq 转化来的
public void export(HttpServletResponse resp, String jsonStr) {
ObjectMapper mapper = new ObjectMapper();
try {
OrderDetailReq req = mapper.readValue(jsonStr, OrderDetailReq.class);
List<OrderDetailResp> detailList = getDetailList(req).getRecords();
//第三个参数是导出后文件名称,最后一个参数指定写用哪个class去写
ExcelUtil.writeExcel(resp, detailList, "xxx详情", "sheet1", OrderDetailResp.class);
} catch (Exception e) {
e.printStackTrace();
}
}
导出实体类对象OrderDetailResp
@