springboot利用easypoi實(shí)現(xiàn)簡(jiǎn)單導(dǎo)出功能
前言
今天玩了一下springboot利用easypoi實(shí)現(xiàn)excel的導(dǎo)出,以前沒玩過導(dǎo)入導(dǎo)出,只不過聽說過看別人用過,怎么說呢,想玩就玩一下吧,畢竟結(jié)合自己業(yè)務(wù)場(chǎng)景需要才會(huì)考慮是否使用。先簡(jiǎn)單介紹一下easypoi。
一、easypoi是什么?
1.不太熟悉poi的
2.不想寫太多重復(fù)太多的
3.只是簡(jiǎn)單的導(dǎo)入導(dǎo)出的
4.喜歡使用模板的
若poi都不知道的童鞋請(qǐng)自行百度。。。
Easypoi的目標(biāo)不是替代poi,而是讓一個(gè)不懂導(dǎo)入導(dǎo)出的快速使用poi完成Excel和word的各種操作,而不是看很多api才可以完成這樣工作。
二、使用步驟
1.傳送門
因?yàn)槲乙彩堑谝淮问褂?,這里還是先將easypoi的文檔放這兒吧:
2.后端springboot
首先引入easypoi所需依賴:
<!--easypoi導(dǎo)入導(dǎo)出--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.1.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>4.1.3</version> </dependency>
或者使用這個(gè):
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>2.1編寫實(shí)體類(我這里是dto,也一樣)
package top.wangxingjun.separate.dto;
import cn.afterturn.easypoi.excel.annotation.Excel;
import top.wangxingjun.separate.dto.base.OutputConverter;
import top.wangxingjun.separate.entity.AdminRole;
import top.wangxingjun.separate.entity.User;
import lombok.Data;
import lombok.ToString;
import java.util.List;
/**
* @author wxj
* @Date 2020/8/10
*/
@Data
@ToString
public class UserDTO implements OutputConverter<UserDTO, User> {
@Excel(name = "編號(hào)")
private int id;
@Excel(name = "賬號(hào)")
private String username;
@Excel(name = "真實(shí)姓名")
private String name;
@Excel(name = "手機(jī)號(hào)")
private String phone;
@Excel(name = "郵箱")
private String email;
@Excel(name = "狀態(tài)",replace = {"啟用_true","禁用_false"})
private boolean enabled;
}
簡(jiǎn)單看一下這個(gè) @Excel 注解主要的值:

關(guān)于圖片路徑
著重說明一下這個(gè)圖片路徑,當(dāng) type 取值為 2 的時(shí)候表示導(dǎo)出為圖片,同時(shí)配合使用的是 imageType 參數(shù),該參數(shù)決定是從 file 讀取,還是去數(shù)據(jù)庫讀取,默認(rèn)為從 file 中讀取,記得很早之前,有小伙伴圖片是直接 base64 存數(shù)據(jù)庫的,不過現(xiàn)在是沒有人干這種事了
2.2控制層
直接看代碼:
/**
* 用戶信息導(dǎo)出
*/
@GetMapping("api/exportUser")
public void exportUser(HttpServletResponse response) throws Exception {
List<UserDTO> list = userService.list();
ExcelUtil.exportExcel(list, null, "sheet1", UserDTO.class, "用戶信息", response);
}
沒錯(cuò),就這么幾行代碼,當(dāng)然了,還要有個(gè)工具類,是我封裝好的,網(wǎng)上也有很多的咯。下面看工具類:
package top.wangxingjun.separate.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @ProjectName: separate
* @Package: top.wangxingjun.separate.util
* @ClassName: ExcelUtil
* @Author: wxj
* @Description: Excel導(dǎo)入導(dǎo)出工具類
* @Date: 2020/8/25 10:07
* @Version: 1.0
*/
@Log4j2
public class ExcelUtil {
/**
* Map集合導(dǎo)出
*
* @param list 需要導(dǎo)出的數(shù)據(jù)
* @param fileName 導(dǎo)出的文件名
* @param response HttpServletResponse對(duì)象
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception{
defaultExport(list, fileName, response);
}
/**
* 復(fù)雜導(dǎo)出Excel,包括文件名以及表名(不創(chuàng)建表頭)
*
* @param list 需要導(dǎo)出的數(shù)據(jù)
* @param title 表格首行標(biāo)題(不需要就傳null)
* @param sheetName 工作表名稱
* @param pojoClass 映射的實(shí)體類
* @param fileName 導(dǎo)出的文件名(如果為null,則默認(rèn)文件名為當(dāng)前時(shí)間戳)
* @param response HttpServletResponse對(duì)象
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
HttpServletResponse response) throws Exception{
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
/**
* 復(fù)雜導(dǎo)出Excel,包括文件名以及表名(創(chuàng)建表頭)
*
* @param list 需要導(dǎo)出的數(shù)據(jù)
* @param title 表格首行標(biāo)題(不需要就傳null)
* @param sheetName 工作表名稱
* @param pojoClass 映射的實(shí)體類
* @param fileName 導(dǎo)出的文件名
* @param isCreateHeader 是否創(chuàng)建表頭
* @param response HttpServletResponse對(duì)象
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
boolean isCreateHeader, HttpServletResponse response) throws Exception{
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* 默認(rèn)導(dǎo)出方法
*
* @param list 需要導(dǎo)出的數(shù)據(jù)
* @param pojoClass 對(duì)應(yīng)的實(shí)體類
* @param fileName 導(dǎo)出的文件名
* @param response HttpServletResponse對(duì)象
* @param exportParams 導(dǎo)出參數(shù)實(shí)體
*/
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
ExportParams exportParams) throws Exception{
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downloadExcel(fileName, workbook, response);
}
/**
* 默認(rèn)導(dǎo)出方法
*
* @param list Map集合
* @param fileName 導(dǎo)出的文件名
* @param response HttpServletResponse對(duì)象
*/
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response)throws Exception {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (null != workbook) {
downloadExcel(fileName, workbook, response);
}
}
/**
* Excel導(dǎo)出
*
* @param fileName Excel導(dǎo)出
* @param workbook Excel對(duì)象
* @param response HttpServletResponse對(duì)象
*/
public static void downloadExcel(String fileName, Workbook workbook, HttpServletResponse response) throws Exception{
try {
if (StringUtils.isEmpty(fileName)) {
throw new RuntimeException("導(dǎo)出文件名不能為空");
}
String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + encodeFileName);
response.setHeader("FileName", encodeFileName);
response.setHeader("Access-Control-Expose-Headers", "FileName");
workbook.write(response.getOutputStream());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 根據(jù)文件路徑來導(dǎo)入Excel
*
* @param filePath 文件路徑
* @param titleRows 表標(biāo)題的行數(shù)
* @param headerRows 表頭行數(shù)
* @param pojoClass 映射的實(shí)體類
* @return
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{
//判斷文件是否存在
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
log.error("模板不能為空", e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return list;
}
/**
* 根據(jù)接收的Excel文件來導(dǎo)入Excel,并封裝成實(shí)體類
*
* @param file 上傳的文件
* @param titleRows 表標(biāo)題的行數(shù)
* @param headerRows 表頭行數(shù)
* @param pojoClass 映射的實(shí)體類
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
log.error("excel文件不能為空", e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return list;
}
/**
* 文件轉(zhuǎn)List
*
* @param file
* @param pojoClass
* @param <T>
* @return
*/
public static <T> List<T> fileToList(MultipartFile file, Class<T> pojoClass) throws Exception{
if (file.isEmpty()) {
throw new RuntimeException("文件為空");
}
List<T> list = ExcelUtil.importExcel(file, 1, 1, pojoClass);
if (CollectionUtils.isEmpty(list)) {
throw new RuntimeException("未解析到表格數(shù)據(jù)");
}
return list;
}
}excel導(dǎo)出所需要的都準(zhǔn)備好了,下面來看一下我的效果:

到此這篇關(guān)于springboot利用easypoi實(shí)現(xiàn)簡(jiǎn)單導(dǎo)出的文章就介紹到這了,更多相關(guān)springboot easypoi導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
IntelliJ?IDEA?2022.2最新版本激活教程(親測(cè)可用版)永久激活工具分享
Jetbrains官方發(fā)布了?IntelliJ?IDEA2022.2?正式版,每次大的版本更新,都會(huì)有較大的調(diào)整和優(yōu)化,除本次更新全面擁抱?Java?17?外,還有對(duì)IDE?UI界面,安全性,便捷性等都做了調(diào)整和優(yōu)化完善,用戶體驗(yàn)提升不少,相信后面會(huì)有不少小伙伴跟著更新2022-08-08
Windows同時(shí)配置兩個(gè)jdk環(huán)境變量的操作步驟
Java Development Kit (JDK) 是開發(fā)Java應(yīng)用程序的基礎(chǔ),包含了編譯器、調(diào)試器以及其他必要的工具,本指南將一步步指導(dǎo)您完成在Windows操作系統(tǒng)上同時(shí)配置兩個(gè)jdk環(huán)境變量的操作步驟,需要的朋友可以參考下2024-09-09
Eclipse中創(chuàng)建Web項(xiàng)目最新方法(2023年)
在Java開發(fā)人員中,最常用的開發(fā)工具應(yīng)該就是Eclipse,下面這篇文章主要給大家介紹了關(guān)于Eclipse中創(chuàng)建Web項(xiàng)目2023年最新的方法,需要的朋友可以參考下2023-09-09

