服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

2020-09-11 00:51Asurplus、 Java教程

这篇文章主要介绍了SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

说到导出 Excel,我们首先会想到 poi、jsxl 等,使用这些工具会显得笨重,学习难度大。今天学习使用 JeecgBoot 中的 Autopoi 导出 Excel,底层基于 easypoi,使用简单,还支持数据字典方式

一、开发前戏

1、引入 maven 依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- AutoPoi Excel工具类-->
<dependency>
  <groupId>org.jeecgframework</groupId>
  <artifactId>autopoi-web</artifactId>
  <version>1.1.1</version>
  <exclusions>
    <exclusion>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
    </exclusion>
  </exclusions>
</dependency>

exclusions 是将 commons-codec 从 autopoi 中排除,避免冲突

2、切换 Jeecg 镜像

以下代码放在 pom.xml 文件中的 parent 标签下面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<repositories>
<repository>
  <id>aliyun</id>
  <name>aliyun Repository</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  <snapshots>
    <enabled>false</enabled>
  </snapshots>
</repository>
<repository>
  <id>jeecg</id>
  <name>jeecg Repository</name>
  <url>http://maven.jeecg.org/nexus/content/repositories/jeecg</url>
  <snapshots>
    <enabled>false</enabled>
  </snapshots>
</repository>
</repositories>

可以看到,这里我们配置了 aliyun 的国内镜像,还配置了 jeecg 的镜像,这样方便我们下载依赖文件

3、导出工具类

我们把导出 Excel 通用方法写在 ExcelUtils.java 文件中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.servlet.ModelAndView;
 
import java.util.List;
 
/**
 * 导出excel工具类
 *
 * @author lizhou
 */
public class ExcelUtils {
 
  /**
   * 导出excel
   *
   * @param title   文件标题
   * @param clazz   实体类型
   * @param exportList 导出数据
   * @param <T>
   * @return
   */
  public static <T> ModelAndView export(String title, Class<T> clazz, List<T> exportList) {
    ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
    mv.addObject(NormalExcelConstants.FILE_NAME, title);
    mv.addObject(NormalExcelConstants.CLASS, clazz);
    mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, title));
    mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
    return mv;
  }
 
}

这样我们导出数据的时候,只需要传入文件的标题(标题同样作为表格的标题)、数据类型、数据集合,就可以导出数据了

二、开始导出

1、给实体类加注解

我们将需要导出的实体类或 VO 类中的属性加上注解 @Excel

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.zyxx.sys.entity;
 
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 org.jeecgframework.poi.excel.annotation.Excel;
 
import java.io.Serializable;
 
/**
 * <p>
 * 用户信息表
 * </p>
 *
 * @author lizhou
 * @since 2020-07-06
 */
@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 Long id;
 
  @Excel(name = "账号", width = 15)
  @ApiModelProperty(value = "登录账号")
  @TableField("account")
  private String account;
 
  @ApiModelProperty(value = "登录密码")
  @TableField("password")
  private String password;
 
  @Excel(name = "姓名", width = 15)
  @ApiModelProperty(value = "姓名")
  @TableField("name")
  private String name;
 
  @Excel(name = "电话", width = 15)
  @ApiModelProperty(value = "电话")
  @TableField("phone")
  private String phone;
 
  @ApiModelProperty(value = "头像")
  @TableField("avatar")
  private String avatar;
 
  @Excel(name = "性别", width = 15)
  @ApiModelProperty(value = "性别(0--未知1--男2--女)")
  @TableField("sex")
  private Integer sex;
 
  @Excel(name = "状态", width = 15)
  @ApiModelProperty(value = "状态(0--正常1--冻结)")
  @TableField("status")
  private Integer status;
 
  @Excel(name = "创建时间", width = 30)
  @ApiModelProperty(value = "创建时间")
  @TableField("create_time")
  private String createTime;
}
  • @Excel(name = “性别”, width = 15)
  • name:表头
  • width:列宽度

导出 Excel 时,只会导出加了 @Excel 注解的字段,不然不会导出

2、导出数据

?
1
2
3
4
5
@ApiOperation(value = "导出用户信息", notes = "导出用户信息")
@GetMapping(value = "/export")
public ModelAndView exportXls(SysUserInfo sysUserInfo) {
  return ExcelUtils.export("用户信息统计报表", SysUserInfo.class, sysUserInfoService.list(1, Integer.MAX_VALUE, sysUserInfo).getData());
}

我们传入了文件的标题,类型为 SysUserInfo,传入了数据的集合,这样我们请求这个 API 就能导出数据了

SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

可以看出数据已经成功导出,但是性别、状态这些属性值还属于魔法值,我们需要自己写 SQL 来翻译这些值,或者配合数据字典来翻译这些值

三、配合数据字典导出

上面介绍了数据的简单导出,下面介绍配合数据字典导出数据,如果对数据字典不熟悉的同学,可先看看我的另一篇博客:【SpringBoot】廿四、SpringBoot中实现数据字典

1、@Excel 注解

与上面注解相比,我们需要多加一个属性,dicCode,如下

?
1
2
3
4
5
@Excel(name = "性别", width = 15, dicCode = "sex")
@ApiModelProperty(value = "性别(0--未知1--男2--女)")
@TableField("sex")
@Dict(dictCode = "sex")
private Integer sex;
  • @Excel(name = “性别”, width = 15, dicCode = “sex”)
  • name:表头
  • width:列宽度
  • dicCode :字典类型

这样,我们就为这个字段注入了一个字典类型,这样就能翻译成文本了

2、配置类

要配合数据字典导出,我们需要配置 autopoi 的配置类 AutoPoiConfig.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * autopoi 配置类
 *
 * @Author Lizhou
 */
 
@Configuration
public class AutoPoiConfig {
    
    /**
     * excel注解字典参数支持(导入导出字典值,自动翻译)
     * 举例: @Excel(name = "性别", width = 15, dicCode = "sex")
     * 1、导出的时候会根据字典配置,把值1,2翻译成:男、女;
     * 2、导入的时候,会把男、女翻译成1,2存进数据库;
     * @return
     */
    @Bean
    public ApplicationContextUtil applicationContextUtil() {
        return new org.jeecgframework.core.util.ApplicationContextUtil();
    }
 
}

3、翻译规则

我们可以根据自己项目中的字典翻译规则,来重写 autopoi 的字典翻译规则 AutoPoiDictService.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import com.zyxx.sys.entity.SysDictDetail;
import com.zyxx.sys.mapper.SysDictDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.dict.service.AutoPoiDictServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 描述:AutoPoi Excel注解支持字典参数设置
 * 举例: @Excel(name = "性别", width = 15, dicCode = "sex")
 * 1、导出的时候会根据字典配置,把值1,2翻译成:男、女;
 * 2、导入的时候,会把男、女翻译成1,2存进数据库;
 *
 * @Author lizhou
 */
@Slf4j
@Service
public class AutoPoiDictService implements AutoPoiDictServiceI {
 
  @Autowired
  private SysDictDetailMapper sysDictDetailMapper;
 
  /**
   * 通过字典翻译字典文本
   *
   * @Author lizhou
   */
  @Override
  public String[] queryDict(String dicTable, String dicCode, String dicText) {
    List<String> dictReplaces = new ArrayList<>();
    List<SysDictDetail> dictList = sysDictDetailMapper.queryDictItemsByCode(dicCode);
    for (SysDictDetail t : dictList) {
      if (t != null) {
        dictReplaces.add(t.getName() + "_" + t.getCode());
      }
    }
    if (dictReplaces != null && dictReplaces.size() != 0) {
      return dictReplaces.toArray(new String[dictReplaces.size()]);
    }
    return null;
  }
}

实现了 AutoPoiDictServiceI 接口,重写 queryDict 方法,这里我只使用了 dicCode 来查询字典列表,这样就能配合数据字典导出了

4、导出数据

导出数据如图所示

SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

可以看出,数据已经成功导出,性别、状态等魔法值已经被翻译成文本,这样,我们的字典翻译是成功的

四、总结

以上介绍了 JeecgBoot 中的 Autopoi 导出 Excel 的方法,还有配合数据字典导出等操作,可以看出,比以往我们使用的 poi、jsxl 使用方便,导出方便,大大提高了我们的工作效率。更多相关SpringBoot Autopoi导出Excel内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_40065776/article/details/107824221

延伸 · 阅读

精彩推荐