EasyPOI实现excel文件导出

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

1.导入依赖

XML 复制代码
        <!-- easypoi导出 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.2.0</version>
        </dependency>

2.创建对应实体类

java 复制代码
package com.wulian.training.center.export;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

@Data
public class TestScoreExportDTO {
    /**
     * 序号
     */
    @Excel(name = "序号", orderNum = "1", width = 15)
    private Integer id;
    /**
     * 人员名称
     */
    @Excel(name = "人员名称", orderNum = "2", width = 15)
    private String personName;
    /**
     * 达标天数
     */
    @Excel(name = "达标天数", orderNum = "3", width = 15)
    private Integer passCount;
    /**
     * 积分总数
     */
    @Excel(name = "积分总数", orderNum = "4", width = 15)
    private String totalScore;
    /**
     * 日平均积分
     */
    @Excel(name = "日平均积分", orderNum = "5", width = 15)
    private String avgScore;
    /**
     * 积分排名
     */
    @Excel(name = "积分排名", orderNum = "6", width = 15)
    private Integer number;

}

3.导出方法

java 复制代码
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public ResultMoudel export(@RequestParam(required = false)String name,HttpServletRequest request) throws IOException {

        Map map=new HashMap();
        map.put("name",name);
        map.put("companyId",sysUser.getCompanyId());
        List<TestScoreExportDTO> list=appTestManageMapper.selectOrderByTypePage(map);
        AtomicInteger id = new AtomicInteger(new Integer(1));
        list.stream().forEach(iter->{
                    iter.setId(id.getAndIncrement());
                });
        String fileName = "积分信息";
        //设置导出参数
        ExportParams exportParams = new ExportParams("积分信息", "排名", ExcelType.XSSF);
        //设置表头
        exportParams.setCreateHeadRows(true);
//        exportParams.setAddIndex(true);
        //数据渲染
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TestScoreExportDTO.class, list);
        //导出
        final String downloadName = new String((fileName + ExcelTypeEnum.XLSX.getValue()).getBytes(), StandardCharsets.ISO_8859_1);
//        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + downloadName);
        //获取输出流,将excel输出
        ServletOutputStream outputStream = response.getOutputStream();
        sheets.write(outputStream);
        return new ResultMoudel<>().success("导出成功");

    }

导出成功:

相关推荐
极客先躯42 分钟前
高级java每日一道面试题-2024年10月3日-分布式篇-分布式系统中的容错策略都有哪些?
java·分布式·版本控制·共识算法·超时重试·心跳检测·容错策略
夜月行者1 小时前
如何使用ssm实现基于SSM的宠物服务平台的设计与实现+vue
java·后端·ssm
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
bin91531 小时前
【EXCEL数据处理】000017 案例 Match和Index函数。
excel
潘多编程1 小时前
Java中的状态机实现:使用Spring State Machine管理复杂状态流转
java·开发语言·spring
_阿伟_2 小时前
SpringMVC
java·spring
代码在改了2 小时前
springboot厨房达人美食分享平台(源码+文档+调试+答疑)
java·spring boot
猿java2 小时前
使用 Kafka面临的挑战
java·后端·kafka
wclass-zhengge2 小时前
数据结构篇(绪论)
java·数据结构·算法
何事驚慌2 小时前
2024/10/5 数据结构打卡
java·数据结构·算法