第一步:新建一个bean对象

  1. package com.tj.paidan.domain;
  2. import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
  3. import com.alibaba.excel.annotation.ExcelProperty;
  4. import com.alibaba.excel.annotation.format.DateTimeFormat;
  5. import com.alibaba.excel.annotation.write.style.ColumnWidth;
  6. import com.baomidou.mybatisplus.annotation.*;
  7. import com.fasterxml.jackson.annotation.JsonFormat;
  8. import lombok.Data;
  9. import java.io.Serializable;
  10. import java.util.Date;
  11. /**
  12. * 根据id导出的排单表
  13. */
  14. @Data
  15. @ExcelIgnoreUnannotated //只有@ExcelProperty注解的才会导出
  16. public class PaidanDown implements Serializable {
  17. //序号
  18. @ExcelProperty(value = "序号") //导出列显示名
  19. @ColumnWidth(5) //列的宽度
  20. private Integer no;
  21. //日期
  22. @ExcelProperty(value = "日期")
  23. @ColumnWidth(10)
  24. @DateTimeFormat(value = "yyyy-MM-dd")
  25. private Date date;
  26. //平台-店铺名称
  27. @ExcelProperty(value = "店铺名称")
  28. @ColumnWidth(10)
  29. private String shop;
  30. private static final long serialVersionUID = 1L;
  31. }

第二步,逻辑处理,下载的是List,表单head是PaidanDown.class

TjFileUtils.downxlsxByEasyexcel参考:excel文件下载

  1. /**
  2. * excel导出 根据勾选的id
  3. *
  4. * @param response
  5. * @param idList id数组
  6. * @throws IOException
  7. */
  8. @Override
  9. public void downxlsxByIds(HttpServletResponse response, List<Long> idList) throws IOException {
  10. //查询条件
  11. LambdaQueryWrapper<PaidanList> lqw = new LambdaQueryWrapper<>();
  12. lqw.in(PaidanList::getId, idList);
  13. //获取数据
  14. List<PaidanList> list = list(lqw);
  15. List<PaidanDown> outList = ListUtils.newArrayList();
  16. if (list.size() > 0) {
  17. log.info("导出的数据:{}", list);
  18. int n = 1;
  19. for (PaidanList paidan : list) {
  20. PaidanDown paidanDown = new PaidanDown();
  21. //查询用户的信息
  22. BaseWorker worker = workerService.getById(paidan.getWorker_id());
  23. paidanDown.setNo(n);
  24. paidanDown.setDate(paidan.getDate());
  25. paidanDown.setShop(paidan.getE_platform_name() + "-" + paidan.getShopname());
  26. outList.add(paidanDown);
  27. n++;
  28. }
  29. //调用封装方法下载xlsx文件
  30. TjFileUtils.downxlsxByEasyexcel(response, PaidanDown.class, outList);
  31. }
  32. }

特殊的格式处理,

可以参考:EasyExcel自定义转换器,常用Format