EasyExcel导出

参考文档 ლ(°◕‵ƹ′◕ლ)
参考文档 ლ(°◕‵ƹ′◕ლ)
参考文档 ლ(°◕‵ƹ′◕ლ)

1.引入依赖 pom

	 
	<!--  避免跟apache引入的依赖冲突,可以排除   -->
	<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.映射实体

@Data
@ApiModel(value = "路线门店操作日志")
public class RouteStoreModifyDto {

    @ExcelIgnore //忽略导出字段
    @ApiModelProperty(value = "主键id")
    private String id;

    @ExcelIgnore
    @ApiModelProperty(value = "预计划编码")
    private String prePlanNo;

    @ColumnWidth(17)//列宽
    @ExcelProperty(value = "门店ID", index = 0) // 导出的字段(跟表头对应)
    @ApiModelProperty(value = "门店id")
    private String storeId;

    @ColumnWidth(17)
    @ExcelProperty(value = "门店名称", index = 1)
    @ApiModelProperty(value = "门店名称")
    private String storeName;

    @ColumnWidth(17)
    @ExcelProperty(value = "操作类型", index = 2)
    @ApiModelProperty(value = "操作类型名称")
    private String operationType;

    @ColumnWidth(17)
    @ExcelProperty(value = "调整前", index = 3)
    @ApiModelProperty(value = "调整前")
    private String modifyBefore;

    @ColumnWidth(17)
    @ExcelProperty(value = "调整后", index = 4)
    @ApiModelProperty(value = "调整后")
    private String modifyAfter;

}

3.代码实例

	httpServletResponse.setContentType("application/x-xls;charset=UTF-8");
	String newName = URLEncoder.encode("门店调整日志导出" + ".xlsx", "UTF-8");
	httpServletResponse.addHeader("Content-Disposition", "attachment;filename=\"" + newName + "\"");

	//没有模板的导出
	EasyExcel.write(httpServletResponse.getOutputStream(), RouteStoreModifyDto.class)
			 .excelType(ExcelTypeEnum.XLSX)
			 .sheet("门店调整日志")
			 .doWrite(data.getList());

	//有模板的导出   excel对应的实体类可以用一个,但是在有模板导出的时候需要设置 .needHead(false) 就不会导致有两个表头了
	ClassPathResource storeInfo = new ClassPathResource("export/门店调整日志导出.xlsx");
	EasyExcel.write(httpServletResponse.getOutputStream(), RouteStoreModifyDto.class)
			.withTemplate(storeInfo.getInputStream())
			.excelType(ExcelTypeEnum.XLSX)
			.sheet()
			.needHead(false)
			.doWrite(data.getList());

4.导出结果

导出结果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值