SpringBoot 导出excel -- EasyPoi

Java代码段

    /**
     * 人员信息导出
     */
    @ApiOperation(value = "导出人员信息列表", notes = "导出人员信息列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "USERID", value = "用户名"),
            @ApiImplicitParam(name = "USERNAME", value = "姓名"),
            @ApiImplicitParam(name = "EMP_CODE", value = "职工号")
    })
    @GetMapping("export")
    public ResultData<List<PersonExcel>> export(@ApiIgnore ModelMap map, HttpServletRequest request, HttpServletResponse response, PersonParams personParams) {
        ResultData<List<PersonExcel>> result = new ResultData<List<PersonExcel>>();
        try {
            List<PersonExcel> list = personService.getPersonList(personParams);
            ExportParams params = new ExportParams(null, "人员信息", ExcelType.XSSF);

            if (!ToolsUtil.isEmpty(list)) {
                map.put(TemplateExcelConstants.FILE_NAME, "人员信息");
                map.put(NormalExcelConstants.DATA_LIST, list);
                map.put(NormalExcelConstants.CLASS, PersonExcel.class);
                map.put(NormalExcelConstants.PARAMS, params);
                PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
                return result.returnMessage(HttpStatus.SUCCESS, "导出成功");
            } else {
                return result.returnMessage(HttpStatus.SUCCESS, "无数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.returnMessage(HttpStatus.ERROR, "导出失败");
    }

PersonExcel

import java.util.Date;

@Data
public class PersonExcel {

    /**
     * ID
     */
    @ExcelIgnore
    private Long id;

    /**
     * 用户名
     */
    @Excel(name = "用户名", width = 20)
    private String userId;

    /**
     * 姓名
     */
    @Excel(name = "姓名", width = 20)
    private String userName;

    /**
     * 职工号
     */
    @Excel(name = "职工号", width = 20)
    private String empCode;

    /**
     * 性别
     */
    @Excel(name = "性别", width = 10)
    private String sex;

    /**
     * 密码
     */
    @ExcelIgnore
    private String password;

    /**
     * 身份证号
     */
    @Excel(name = "身份证号", width = 30)
    private String personCode;

    /**
     * 相片
     */
    @ExcelIgnore
    private String photo;

    /**
     * 手机号
     */
    @Excel(name = "手机", width = 30)
    private Long mobilePhone;

    /**
     * 固定电话
     */
    @Excel(name = "座机", width = 30)
    private Long phone;


}

pom.xml (全用比较保险吧,懒得去测试排除哪些不需要了)

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.4.0</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.13</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值