最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟

 更新時(shí)間:2020年09月10日 11:52:49   作者:Asurplus、  
這篇文章主要介紹了SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

說到導(dǎo)出 Excel,我們首先會(huì)想到 poi、jsxl 等,使用這些工具會(huì)顯得笨重,學(xué)習(xí)難度大。今天學(xué)習(xí)使用 JeecgBoot 中的 Autopoi 導(dǎo)出 Excel,底層基于 easypoi,使用簡(jiǎn)單,還支持?jǐn)?shù)據(jù)字典方式

一、開發(fā)前戲

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 標(biāo)簽下面

<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 的國(guó)內(nèi)鏡像,還配置了 jeecg 的鏡像,這樣方便我們下載依賴文件

3、導(dǎo)出工具類

我們把導(dǎo)出 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;

/**
 * 導(dǎo)出excel工具類
 *
 * @author lizhou
 */
public class ExcelUtils {

  /**
   * 導(dǎo)出excel
   *
   * @param title   文件標(biāo)題
   * @param clazz   實(shí)體類型
   * @param exportList 導(dǎo)出數(shù)據(jù)
   * @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;
  }

}

這樣我們導(dǎo)出數(shù)據(jù)的時(shí)候,只需要傳入文件的標(biāo)題(標(biāo)題同樣作為表格的標(biāo)題)、數(shù)據(jù)類型、數(shù)據(jù)集合,就可以導(dǎo)出數(shù)據(jù)了

二、開始導(dǎo)出

1、給實(shí)體類加注解

我們將需要導(dǎo)出的實(shí)體類或 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對(duì)象", description = "用戶信息表")
public class SysUserInfo extends Model<SysUserInfo> {


  @ApiModelProperty(value = "ID")
  @TableId(value = "id", type = IdType.AUTO)
  private Long id;

  @Excel(name = "賬號(hào)", width = 15)
  @ApiModelProperty(value = "登錄賬號(hào)")
  @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 = "狀態(tài)", width = 15)
  @ApiModelProperty(value = "狀態(tài)(0--正常1--凍結(jié))")
  @TableField("status")
  private Integer status;

  @Excel(name = "創(chuàng)建時(shí)間", width = 30)
  @ApiModelProperty(value = "創(chuàng)建時(shí)間")
  @TableField("create_time")
  private String createTime;
}
  • @Excel(name = “性別”, width = 15)
  • name:表頭
  • width:列寬度

導(dǎo)出 Excel 時(shí),只會(huì)導(dǎo)出加了 @Excel 注解的字段,不然不會(huì)導(dǎo)出

2、導(dǎo)出數(shù)據(jù)

@ApiOperation(value = "導(dǎo)出用戶信息", notes = "導(dǎo)出用戶信息")
@GetMapping(value = "/export")
public ModelAndView exportXls(SysUserInfo sysUserInfo) {
  return ExcelUtils.export("用戶信息統(tǒng)計(jì)報(bào)表", SysUserInfo.class, sysUserInfoService.list(1, Integer.MAX_VALUE, sysUserInfo).getData());
}

我們傳入了文件的標(biāo)題,類型為 SysUserInfo,傳入了數(shù)據(jù)的集合,這樣我們請(qǐng)求這個(gè) API 就能導(dǎo)出數(shù)據(jù)了



可以看出數(shù)據(jù)已經(jīng)成功導(dǎo)出,但是性別、狀態(tài)這些屬性值還屬于魔法值,我們需要自己寫 SQL 來翻譯這些值,或者配合數(shù)據(jù)字典來翻譯這些值

三、配合數(shù)據(jù)字典導(dǎo)出

上面介紹了數(shù)據(jù)的簡(jiǎn)單導(dǎo)出,下面介紹配合數(shù)據(jù)字典導(dǎo)出數(shù)據(jù),如果對(duì)數(shù)據(jù)字典不熟悉的同學(xué),可先看看我的另一篇博客:【SpringBoot】廿四、SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典

1、@Excel 注解

與上面注解相比,我們需要多加一個(gè)屬性,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 :字典類型

這樣,我們就為這個(gè)字段注入了一個(gè)字典類型,這樣就能翻譯成文本了

2、配置類

要配合數(shù)據(jù)字典導(dǎo)出,我們需要配置 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注解字典參數(shù)支持(導(dǎo)入導(dǎo)出字典值,自動(dòng)翻譯)
	 * 舉例: @Excel(name = "性別", width = 15, dicCode = "sex")
	 * 1、導(dǎo)出的時(shí)候會(huì)根據(jù)字典配置,把值1,2翻譯成:男、女;
	 * 2、導(dǎo)入的時(shí)候,會(huì)把男、女翻譯成1,2存進(jìn)數(shù)據(jù)庫(kù);
	 * @return
	 */
	@Bean
	public ApplicationContextUtil applicationContextUtil() {
		return new org.jeecgframework.core.util.ApplicationContextUtil();
	}

}

3、翻譯規(guī)則

我們可以根據(jù)自己項(xiàng)目中的字典翻譯規(guī)則,來重寫 autopoi 的字典翻譯規(guī)則 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注解支持字典參數(shù)設(shè)置
 * 舉例: @Excel(name = "性別", width = 15, dicCode = "sex")
 * 1、導(dǎo)出的時(shí)候會(huì)根據(jù)字典配置,把值1,2翻譯成:男、女;
 * 2、導(dǎo)入的時(shí)候,會(huì)把男、女翻譯成1,2存進(jìn)數(shù)據(jù)庫(kù);
 *
 * @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;
  }
}

實(shí)現(xiàn)了 AutoPoiDictServiceI 接口,重寫 queryDict 方法,這里我只使用了 dicCode 來查詢字典列表,這樣就能配合數(shù)據(jù)字典導(dǎo)出了

4、導(dǎo)出數(shù)據(jù)

導(dǎo)出數(shù)據(jù)如圖所示


可以看出,數(shù)據(jù)已經(jīng)成功導(dǎo)出,性別、狀態(tài)等魔法值已經(jīng)被翻譯成文本,這樣,我們的字典翻譯是成功的

四、總結(jié)

以上介紹了 JeecgBoot 中的 Autopoi 導(dǎo)出 Excel 的方法,還有配合數(shù)據(jù)字典導(dǎo)出等操作,可以看出,比以往我們使用的 poi、jsxl 使用方便,導(dǎo)出方便,大大提高了我們的工作效率。更多相關(guān)SpringBoot Autopoi導(dǎo)出Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java設(shè)置session過期時(shí)間的實(shí)現(xiàn)方法

    java設(shè)置session過期時(shí)間的實(shí)現(xiàn)方法

    這篇文章主要介紹了java設(shè)置session過期時(shí)間的實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講述了具體實(shí)現(xiàn)過程,非常具有參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 探討:使用httpClient在客戶端與服務(wù)器端傳輸對(duì)象參數(shù)的詳解

    探討:使用httpClient在客戶端與服務(wù)器端傳輸對(duì)象參數(shù)的詳解

    本篇文章是對(duì)使用httpClient在客戶端與服務(wù)器端傳輸對(duì)象參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • SpringBoot中通過實(shí)現(xiàn)WebMvcConfigurer參數(shù)校驗(yàn)的方法示例

    SpringBoot中通過實(shí)現(xiàn)WebMvcConfigurer參數(shù)校驗(yàn)的方法示例

    這篇文章主要介紹了SpringBoot中通過實(shí)現(xiàn)WebMvcConfigurer參數(shù)校驗(yàn)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 解決使用this.getClass().getResource()獲取文件時(shí)遇到的坑

    解決使用this.getClass().getResource()獲取文件時(shí)遇到的坑

    這篇文章主要介紹了解決使用this.getClass().getResource()獲取文件時(shí)遇到的坑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    隨著摩爾定律逐步失效,cpu單核性能達(dá)到瓶頸,并發(fā)逐漸逐漸得到廣泛應(yīng)用,因而學(xué)習(xí)了解以及使用并發(fā)就顯得十分重要,但并發(fā)相關(guān)的知識(shí)比較瑣碎,不易系統(tǒng)學(xué)習(xí),因而本篇文章參照王寶令老師《Java并發(fā)編程》來勾勒出一張“并發(fā)全景圖”,需要的朋友可以參考下
    2021-05-05
  • Opencv實(shí)現(xiàn)身份證OCR識(shí)別的示例詳解

    Opencv實(shí)現(xiàn)身份證OCR識(shí)別的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Opencv實(shí)現(xiàn)身份證OCR識(shí)別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2024-03-03
  • SpringBoot中的6種API請(qǐng)求參數(shù)讀取方式總結(jié)

    SpringBoot中的6種API請(qǐng)求參數(shù)讀取方式總結(jié)

    使用Spring Boot開發(fā)API的時(shí)候,讀取請(qǐng)求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,Spring Boot中也提供了多種機(jī)制來滿足不同的API設(shè)計(jì)要求,通過本文,為大家總結(jié)6種常用的請(qǐng)求參數(shù)讀取方式,需要的朋友可以參考下
    2024-07-07
  • Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼

    Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)網(wǎng)絡(luò)資源的斷點(diǎn)續(xù)傳功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下
    2022-10-10
  • 軟件開發(fā)基礎(chǔ)之設(shè)計(jì)模式概述

    軟件開發(fā)基礎(chǔ)之設(shè)計(jì)模式概述

    這篇文章介紹了軟件開發(fā)基礎(chǔ)之設(shè)計(jì)模式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • 詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯

    詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯

    這篇文章主要介紹了詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07

最新評(píng)論

米林县| 天气| 汉寿县| 台东县| 辰溪县| 油尖旺区| 宝山区| 上犹县| 大竹县| 和硕县| 库尔勒市| 稷山县| 平阳县| 宜黄县| 南昌县| 营口市| 积石山| 安西县| 临沧市| 沙坪坝区| 赣州市| 瓦房店市| 瓮安县| 义马市| 盘山县| 兴仁县| 兴义市| 兴安盟| 当阳市| 汉阴县| 汝南县| 美姑县| 大连市| 曲靖市| 英超| 遂溪县| 台中市| 封丘县| 达尔| 河曲县| 白山市|