第一步:新建一个bean对象
package com.tj.paidan.domain;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 根据id导出的排单表
*/
@Data
@ExcelIgnoreUnannotated //只有@ExcelProperty注解的才会导出
public class PaidanDown implements Serializable {
//序号
@ExcelProperty(value = "序号") //导出列显示名
@ColumnWidth(5) //列的宽度
private Integer no;
//日期
@ExcelProperty(value = "日期")
@ColumnWidth(10)
@DateTimeFormat(value = "yyyy-MM-dd")
private Date date;
//平台-店铺名称
@ExcelProperty(value = "店铺名称")
@ColumnWidth(10)
private String shop;
private static final long serialVersionUID = 1L;
}
第二步,逻辑处理,下载的是List,表单head是PaidanDown.class
TjFileUtils.downxlsxByEasyexcel参考:excel文件下载
/**
* excel导出 根据勾选的id
*
* @param response
* @param idList id数组
* @throws IOException
*/
@Override
public void downxlsxByIds(HttpServletResponse response, List<Long> idList) throws IOException {
//查询条件
LambdaQueryWrapper<PaidanList> lqw = new LambdaQueryWrapper<>();
lqw.in(PaidanList::getId, idList);
//获取数据
List<PaidanList> list = list(lqw);
List<PaidanDown> outList = ListUtils.newArrayList();
if (list.size() > 0) {
log.info("导出的数据:{}", list);
int n = 1;
for (PaidanList paidan : list) {
PaidanDown paidanDown = new PaidanDown();
//查询用户的信息
BaseWorker worker = workerService.getById(paidan.getWorker_id());
paidanDown.setNo(n);
paidanDown.setDate(paidan.getDate());
paidanDown.setShop(paidan.getE_platform_name() + "-" + paidan.getShopname());
outList.add(paidanDown);
n++;
}
//调用封装方法下载xlsx文件
TjFileUtils.downxlsxByEasyexcel(response, PaidanDown.class, outList);
}
}