irpas技术客

【SpringBoot】37、SpringBoot整合EasyPoi自定义字典导出Excel_Asurplus_easypoi字典

大大的周 1355

前面我们介绍了 SpringBoot 中使用 JeecgBoot 的 Autopoi 导出 Excel,其实 Autopoi 的底层也是 EasyPoi,对于 Excel 的导入/导出也是非常方便的。那 EasyPoi 也是基于 POI 的,如果对这方面想要深究的,可以先看看原生 POI 的导入/导出方式,你会回来选择 EasyPoi 的

一、简介

EasyPoi 功能如同名字 easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出 Excel 导出,Excel 模板导出,Excel 导入,Word 模板导出,通过简单的注解和模板语言(熟悉的表达式语法),完成以前复杂的写法

如果想了解 JeecgBoot 的 Autopoi,可以参考我的另一篇博客,SpringBoot 中使用 JeecgBoot 的 Autopoi 导出 Excel

https://blog.csdn.net/qq_40065776/article/details/107824221 二、引入 EasyPoi

EasyPoi 在 SpringBoot 中也是做了很好的封装,让我们能够在 SpringBoot 快速地使用 EasyPoi 进行开发

<!-- easypoi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.2.0</version> </dependency>

我们只需要引入这一个依赖即可,这是对 SpringBoot 做了很好的支持

三、源码解读 1、@Excel 源码解读

通过查阅源码,我们不难从 cn.afterturn.easypoi.excel.annotation.Excel 注解中发现

/** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://·mon.excel; import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; import com.zyxx.sys.service.SysDictDetailService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /** * 支持字典参数设置 * 举例: @Excel(name = "性别", width = 15, dicCode = "sex") * 1、导出的时候会根据字典配置,把值1,2翻译成:男、女; * 2、导入的时候,会把男、女翻译成1,2存进数据库; * * @Author lizhou */ @Slf4j @Component public class IExcelDictHandlerImpl implements IExcelDictHandler { @Autowired private SysDictDetailMapper testSysDictDetailMapper; private static SysDictDetailMapper sysDictDetailMapper; @PostConstruct public void init() { sysDictDetailMapper = this.testSysDictDetailMapper; } /** * 从值翻译到名称 * * @param dict 字典Key * @param obj 对象 * @param name 属性名称 * @param value 属性值 * @return */ @Override public String toName(String dict, Object obj, String name, Object value) { return sysDictDetailMapper.getTextByDictAndValue(dict, String.valueOf(value)); } /** * 从名称翻译到值 * * @param dict 字典Key * @param obj 对象 * @param name 属性名称 * @param value 属性值 * @return */ @Override public String toValue(String dict, Object obj, String name, Object value) { return null; } } 1、这里我们导出,只使用了 toName(从值翻译到名称)这个方法,所以,只写了一个方法2、我们需要使用 @Component 注解将它加载到 Spring 容器中3、@PostConstruct 该注解被用来修饰一个非静态的 void() 方法。被 @PostConstruct 修饰的方法会在服务器加载 Servlet 的时候运行,并且只会被服务器执行一次。PostConstruct 在构造函数之后执行,init() 方法之前执行 四、开始导出 1、定义实体类 package com.zyxx.sys.entity; import cn.afterturn.easypoi.excel.annotation.Excel; import com.baomidou.mybatisplus.annotation.*; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.zyxx.common.annotation.Dict; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import java.io.Serializable; import java.util.Date; /** * <p> * 用户信息表 * </p> * * @author lizhou */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("sys_user_info") @ApiModel(value = "SysUserInfo对象", description = "用户信息表") public class SysUserInfo extends Model<SysUserInfo> { @ApiModelProperty(value = "ID") @TableId(value = "id", type = IdType.AUTO) private Integer id; @Excel(name = "姓名", width = 15) @ApiModelProperty(value = "姓名") @TableField("name") private String name; @Excel(name = "电话", width = 15) @ApiModelProperty(value = "电话") @TableField("phone") private String phone; @Excel(name = "性别", width = 15, dict = "sex") @TableField("sex") @Dict(dictCode = "user_sex") private Integer sex; @Excel(name = "状态", width = 15, dict = "status") @TableField("status") private Integer status; }

@Excel 注解解释如下:

name,表头名称width,列宽dict,字典 key 2、导出 API 接口

controller 层提供导出 API

@ApiOperation(value = "导出用户信息", notes = "导出用户信息") @GetMapping(value = "/export") public void exportXls(HttpServletResponse response) { // 查询数据 List<SysUserInfo> list = sysUserInfoService.list(1, Integer.MAX_VALUE); // 导出数据,数据,数据类型,文件名称,表名,响应对象 ExportExcelUtil.exportExcel(list, SysUserInfo.class, "用户信息表", "用户信息统计", response); } 3、导出工具类 /** * 导出excel * * @param list 数据集合 * @param pojoClass 数据类型 * @param fileName 文件名称 * @param title 表明 * @param response 响应对象 */ public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, String title, HttpServletResponse response) { ExportParams exportParams = new ExportParams(title, null); // 自定义字典查询规则 exportParams.setDictHandler(new IExcelDictHandlerImpl()); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); if (workbook != null) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }

exportParams.setDictHandler(new IExcelDictHandlerImpl());,我们传入了自定义的字典查询规则

五、测试导出

我们调取导出数据的 API 接口,即可导出文件,导出效果如下:

六、总结

可以看出,自定义字典查询导出方式,其实和 JeecgBoot 的 Autopoi 方式都大同小异,后面是发现了 JeecgBoot 的 Autopoi 和 hutool 的读取文件 ExcelReader 有冲突,放弃了 JeecgBoot 的 Autopoi,EasyPoi 确实是一款强大的 Excel 操作产品!!!

如您在阅读中发现不足,欢迎留言!!!


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #easypoi字典 #前面我们介绍了 #springboot #中使用 #jeecgboot # #Autopoi #导出