参考文档 ლ(°◕‵ƹ′◕ლ)
参考文档 ლ(°◕‵ƹ′◕ლ)
参考文档 ლ(°◕‵ƹ′◕ლ)
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.导出结果