• EasyPOI实现excel文件导出


    EasyPOI真的是一款非常好用的文件导出工具,相较于传统的一行一列的数据导出,这种以实体类绑定生成的方式真的非常方便,也希望大家能够了解、掌握其使用方法,下面就用一个实例来简单介绍一下EasyPOI的使用。

    1.导入依赖

    1. <dependency>
    2. <groupId>cn.afterturngroupId>
    3. <artifactId>easypoi-baseartifactId>
    4. <version>4.2.0version>
    5. dependency>
    6. <dependency>
    7. <groupId>cn.afterturngroupId>
    8. <artifactId>easypoi-webartifactId>
    9. <version>4.2.0version>
    10. dependency>
    11. <dependency>
    12. <groupId>cn.afterturngroupId>
    13. <artifactId>easypoi-annotationartifactId>
    14. <version>4.2.0version>
    15. dependency>

    2.创建对应实体类

    1. package com.wulian.training.center.export;
    2. import cn.afterturn.easypoi.excel.annotation.Excel;
    3. import lombok.Data;
    4. @Data
    5. public class TestScoreExportDTO {
    6. /**
    7. * 序号
    8. */
    9. @Excel(name = "序号", orderNum = "1", width = 15)
    10. private Integer id;
    11. /**
    12. * 人员名称
    13. */
    14. @Excel(name = "人员名称", orderNum = "2", width = 15)
    15. private String personName;
    16. /**
    17. * 达标天数
    18. */
    19. @Excel(name = "达标天数", orderNum = "3", width = 15)
    20. private Integer passCount;
    21. /**
    22. * 积分总数
    23. */
    24. @Excel(name = "积分总数", orderNum = "4", width = 15)
    25. private String totalScore;
    26. /**
    27. * 日平均积分
    28. */
    29. @Excel(name = "日平均积分", orderNum = "5", width = 15)
    30. private String avgScore;
    31. /**
    32. * 积分排名
    33. */
    34. @Excel(name = "积分排名", orderNum = "6", width = 15)
    35. private Integer number;
    36. }

    3.导出方法
     

    1. @PostMapping("/export")
    2. @ApiOperation(value = "导出")
    3. public ResultMoudel export(@RequestParam(required = false)String name,HttpServletRequest request) throws IOException {
    4. Map map=new HashMap();
    5. map.put("name",name);
    6. map.put("companyId",sysUser.getCompanyId());
    7. List list=appTestManageMapper.selectOrderByTypePage(map);
    8. AtomicInteger id = new AtomicInteger(new Integer(1));
    9. list.stream().forEach(iter->{
    10. iter.setId(id.getAndIncrement());
    11. });
    12. String fileName = "积分信息";
    13. //设置导出参数
    14. ExportParams exportParams = new ExportParams("积分信息", "排名", ExcelType.XSSF);
    15. //设置表头
    16. exportParams.setCreateHeadRows(true);
    17. // exportParams.setAddIndex(true);
    18. //数据渲染
    19. Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TestScoreExportDTO.class, list);
    20. //导出
    21. final String downloadName = new String((fileName + ExcelTypeEnum.XLSX.getValue()).getBytes(), StandardCharsets.ISO_8859_1);
    22. // response.setCharacterEncoding("UTF-8");
    23. response.setHeader("content-Type", "application/vnd.ms-excel");
    24. response.setHeader("Content-Disposition", "attachment;filename=" + downloadName);
    25. //获取输出流,将excel输出
    26. ServletOutputStream outputStream = response.getOutputStream();
    27. sheets.write(outputStream);
    28. return new ResultMoudel<>().success("导出成功");
    29. }

     导出成功:

  • 相关阅读:
    防勒索病毒的十种方案
    JUC并发编程——CAS与原子引用(基于狂神说的学习笔记)
    金蝶云苍穹-插件开发(四)GPT开发相关插件
    【WSN通信】基于最佳簇半径的无线传感器网络分簇路由算法附matlab代码
    并发编程的核心问题
    复杂环境下多移动机器人路径规划研究(Matlab代码实现)
    Android学习笔记 2.2.3 帧布局 && 2.2.4 绝对布局
    BOM系列之localStorage
    springboot毕设项目车位预定管理系统76ov7(java+VUE+Mybatis+Maven+Mysql)
    计算机毕业设计-微信小程序文学小说阅读销售系统
  • 原文地址:https://blog.csdn.net/qq_44038822/article/details/134377464