SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟
說到導(dǎo)出 Excel,我們首先會想到 poi、jsxl 等,使用這些工具會顯得笨重,學(xué)習(xí)難度大。今天學(xué)習(xí)使用 JeecgBoot 中的 Autopoi 導(dǎo)出 Excel,底層基于 easypoi,使用簡單,還支持?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 的國內(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 實體類型 * @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ù)的時候,只需要傳入文件的標(biāo)題(標(biāo)題同樣作為表格的標(biāo)題)、數(shù)據(jù)類型、數(shù)據(jù)集合,就可以導(dǎo)出數(shù)據(jù)了
二、開始導(dǎo)出
1、給實體類加注解
我們將需要導(dǎo)出的實體類或 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 = "狀態(tài)", width = 15) @ApiModelProperty(value = "狀態(tài)(0--正常1--凍結(jié))") @TableField("status") private Integer status; @Excel(name = "創(chuàng)建時間", width = 30) @ApiModelProperty(value = "創(chuàng)建時間") @TableField("create_time") private String createTime; }
- @Excel(name = “性別”, width = 15)
- name:表頭
- width:列寬度
導(dǎo)出 Excel 時,只會導(dǎo)出加了 @Excel 注解的字段,不然不會導(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)計報表", SysUserInfo.class, sysUserInfoService.list(1, Integer.MAX_VALUE, sysUserInfo).getData()); }
我們傳入了文件的標(biāo)題,類型為 SysUserInfo,傳入了數(shù)據(jù)的集合,這樣我們請求這個 API 就能導(dǎo)出數(shù)據(jù)了
可以看出數(shù)據(jù)已經(jīng)成功導(dǎo)出,但是性別、狀態(tài)這些屬性值還屬于魔法值,我們需要自己寫 SQL 來翻譯這些值,或者配合數(shù)據(jù)字典來翻譯這些值
三、配合數(shù)據(jù)字典導(dǎo)出
上面介紹了數(shù)據(jù)的簡單導(dǎo)出,下面介紹配合數(shù)據(jù)字典導(dǎo)出數(shù)據(jù),如果對數(shù)據(jù)字典不熟悉的同學(xué),可先看看我的另一篇博客:【SpringBoot】廿四、SpringBoot中實現(xiàn)數(shù)據(jù)字典
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、配置類
要配合數(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)出字典值,自動翻譯) * 舉例: @Excel(name = "性別", width = 15, dicCode = "sex") * 1、導(dǎo)出的時候會根據(jù)字典配置,把值1,2翻譯成:男、女; * 2、導(dǎo)入的時候,會把男、女翻譯成1,2存進數(shù)據(jù)庫; * @return */ @Bean public ApplicationContextUtil applicationContextUtil() { return new org.jeecgframework.core.util.ApplicationContextUtil(); } }
3、翻譯規(guī)則
我們可以根據(jù)自己項目中的字典翻譯規(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)出的時候會根據(jù)字典配置,把值1,2翻譯成:男、女; * 2、導(dǎo)入的時候,會把男、女翻譯成1,2存進數(shù)據(jù)庫; * * @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; } }
實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成JWT實現(xiàn)登陸驗證的方法詳解
JSON?Web?Token(JWT)是一個開放的標(biāo)準(zhǔn)(RFC?7519),它定義了一個緊湊且自包含的方式,用于在各方之間以JSON對象安全地傳輸信息。本文將利用SpringBoot集成JWT實現(xiàn)登陸驗證,感興趣的可以了解一下2022-05-05Java C++題解leetcode886可能的二分法并查集染色法
這篇文章主要為大家介紹了Java C++題解leetcode886可能的二分法并查集染色法實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Java 最優(yōu)二叉樹的哈夫曼算法的簡單實現(xiàn)
這篇文章主要介紹了Java 最優(yōu)二叉樹的哈夫曼算法的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10