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("导出成功");

    }

导出成功:

相关推荐
非 白2 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼8 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒11 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
Forget the Dream40 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek43 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar