温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot使用JeecgBoot中的Autopoi功能如何实现导出Excel

发布时间:2020-11-04 15:37:45 来源:亿速云 阅读:4637 作者:Leah 栏目:开发技术

这篇文章运用简单易懂的例子给大家介绍SpringBoot使用JeecgBoot中的Autopoi功能如何实现导出Excel,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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

一、开发前戏

1、引入 maven 依赖

<!-- 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 标签下面

<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 文件中

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

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、导出数据

@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,如下

@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

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

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

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

关于SpringBoot使用JeecgBoot中的Autopoi功能如何实现导出Excel就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI