小试阿里巴巴EasyExcel导出Excel

添加easyexcel的maven依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.1</version>
</dependency>

导出的模型需要继承BaseRowModel,然后可以在每个属性上设置所在的列和一些format

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

public class GoodsOrderExportDto extends BaseRowModel {

@ExcelProperty(value = "订单序号",index = 0)
private Long orderId;

@ExcelProperty(value = "订单编号",index = 1)
private String orderCode;

@ExcelProperty(value = "商品序号",index = 2)
private Long goodsId;

@ExcelProperty(value = "商品名称",index = 3)
private String goodsTitle;

@ExcelProperty(value = "商品链接",index = 4)
private String goodsDetailsUrl;

@ExcelProperty(value = "商品图片",index = 5)
private String goodsPhoto;

@ExcelProperty(value = "商品数量",index = 6)
private Long goodsNum;

@ExcelProperty(value = "商品价格",index = 7)
private BigDecimal goodsAmt;

@ExcelProperty(value = "区域公司",index = 8)
private Long areaComp;

@ExcelProperty(value = "付款时间",index = 12,format = "yy-MM-dd hh:mm:ss")
private Date payTime;

}

最后在控制器输出

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

@RequestMapping(value = "export",method = RequestMethod.GET)
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {

List<GoodsOrderDto> orderDtoList = goodsOrderServiceRemote.querGoodsOrderList();
List<GoodsOrderExportDto> orderExportDtoList = Collections.emptyList();
if (CollectionUtils.isNotEmpty(orderDtoList)){
orderExportDtoList = new ArrayList<>(orderDtoList.size());
for (GoodsOrderDto goodsOrderDto : orderDtoList) {
GoodsOrderExportDto goodsOrderExportDto = new GoodsOrderExportDto();
BeanUtils.copyProperties(goodsOrderDto,goodsOrderExportDto);

orderExportDtoList.add(goodsOrderExportDto);
}
orderDtoList.clear();
}

// 设定输出文件头
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("订单导出.xls", "UTF-8"));
// 定义输出类型
response.setContentType("application/msexcel");

OutputStream outputStream = response.getOutputStream();

ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLS);

Sheet sheet = new Sheet(1, 0, GoodsOrderExportDto.class);

excelWriter.write(orderExportDtoList,sheet);

outputStream.flush();
excelWriter.finish();
outputStream.close();
}
  • 作者: Sam
  • 发布时间: 2019-05-17 23:43:39
  • 最后更新: 2019-12-09 23:03:26
  • 文章链接: https://ydstudios.gitee.io/post/76eb7bfd.html
  • 版权声明: 本网所有文章除特别声明外, 禁止未经授权转载,违者依法追究相关法律责任!