栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

一、开发前戏

1、引入 maven 依赖



  org.jeecgframework
  autopoi-web
  1.1.1
  
    
      commons-codec
      commons-codec
    
  

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

2、切换 Jeecg 镜像

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



  aliyun
  aliyun Repository
  http://maven.aliyun.com/nexus/content/groups/public
  
    false
  


  jeecg
  jeecg Repository
  http://maven.jeecg.org/nexus/content/repositories/jeecg
  
    false
  


可以看到,这里我们配置了 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;


public class ExcelUtils {

  
  public static  ModelAndView export(String title, Class clazz, List 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;


@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user_info")
@ApiModel(value = "SysUserInfo对象", description = "用户信息表")
public class SysUserInfo extends Model {


  @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 就能导出数据了



可以看出数据已经成功导出,但是性别、状态这些属性值还属于魔法值,我们需要自己写 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;



@Configuration
public class AutoPoiConfig {
	
	
	@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;


@Slf4j
@Service
public class AutoPoiDictService implements AutoPoiDictServiceI {

  @Autowired
  private SysDictDetailMapper sysDictDetailMapper;

  
  @Override
  public String[] queryDict(String dicTable, String dicCode, String dicText) {
    List dictReplaces = new ArrayList<>();
    List 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、导出数据

导出数据如图所示


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

四、总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号