springboot利用easypoi實現簡單導出功能
前言
今天玩了一下springboot利用easypoi實現excel的導出,以前沒玩過導入導出,只不過聽說過看別人用過,怎么說呢,想玩就玩一下吧,畢竟結合自己業(yè)務場景需要才會考慮是否使用。先簡單介紹一下easypoi。
一、easypoi是什么?
1.不太熟悉poi的
2.不想寫太多重復太多的
3.只是簡單的導入導出的
4.喜歡使用模板的
若poi都不知道的童鞋請自行百度。。。
Easypoi的目標不是替代poi,而是讓一個不懂導入導出的快速使用poi完成Excel和word的各種操作,而不是看很多api才可以完成這樣工作。
二、使用步驟
1.傳送門
因為我也是第一次使用,這里還是先將easypoi的文檔放這兒吧:
2.后端springboot
首先引入easypoi所需依賴:
<!--easypoi導入導出--> <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>
或者使用這個:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>2.1編寫實體類(我這里是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 = "編號")
private int id;
@Excel(name = "賬號")
private String username;
@Excel(name = "真實姓名")
private String name;
@Excel(name = "手機號")
private String phone;
@Excel(name = "郵箱")
private String email;
@Excel(name = "狀態(tài)",replace = {"啟用_true","禁用_false"})
private boolean enabled;
}
簡單看一下這個 @Excel 注解主要的值:

關于圖片路徑
著重說明一下這個圖片路徑,當 type 取值為 2 的時候表示導出為圖片,同時配合使用的是 imageType 參數,該參數決定是從 file 讀取,還是去數據庫讀取,默認為從 file 中讀取,記得很早之前,有小伙伴圖片是直接 base64 存數據庫的,不過現在是沒有人干這種事了
2.2控制層
直接看代碼:
/**
* 用戶信息導出
*/
@GetMapping("api/exportUser")
public void exportUser(HttpServletResponse response) throws Exception {
List<UserDTO> list = userService.list();
ExcelUtil.exportExcel(list, null, "sheet1", UserDTO.class, "用戶信息", response);
}
沒錯,就這么幾行代碼,當然了,還要有個工具類,是我封裝好的,網上也有很多的咯。下面看工具類:
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導入導出工具類
* @Date: 2020/8/25 10:07
* @Version: 1.0
*/
@Log4j2
public class ExcelUtil {
/**
* Map集合導出
*
* @param list 需要導出的數據
* @param fileName 導出的文件名
* @param response HttpServletResponse對象
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception{
defaultExport(list, fileName, response);
}
/**
* 復雜導出Excel,包括文件名以及表名(不創(chuàng)建表頭)
*
* @param list 需要導出的數據
* @param title 表格首行標題(不需要就傳null)
* @param sheetName 工作表名稱
* @param pojoClass 映射的實體類
* @param fileName 導出的文件名(如果為null,則默認文件名為當前時間戳)
* @param response HttpServletResponse對象
*/
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));
}
/**
* 復雜導出Excel,包括文件名以及表名(創(chuàng)建表頭)
*
* @param list 需要導出的數據
* @param title 表格首行標題(不需要就傳null)
* @param sheetName 工作表名稱
* @param pojoClass 映射的實體類
* @param fileName 導出的文件名
* @param isCreateHeader 是否創(chuàng)建表頭
* @param response HttpServletResponse對象
*/
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);
}
/**
* 默認導出方法
*
* @param list 需要導出的數據
* @param pojoClass 對應的實體類
* @param fileName 導出的文件名
* @param response HttpServletResponse對象
* @param exportParams 導出參數實體
*/
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);
}
/**
* 默認導出方法
*
* @param list Map集合
* @param fileName 導出的文件名
* @param response HttpServletResponse對象
*/
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導出
*
* @param fileName Excel導出
* @param workbook Excel對象
* @param response HttpServletResponse對象
*/
public static void downloadExcel(String fileName, Workbook workbook, HttpServletResponse response) throws Exception{
try {
if (StringUtils.isEmpty(fileName)) {
throw new RuntimeException("導出文件名不能為空");
}
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);
}
}
/**
* 根據文件路徑來導入Excel
*
* @param filePath 文件路徑
* @param titleRows 表標題的行數
* @param headerRows 表頭行數
* @param pojoClass 映射的實體類
* @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;
}
/**
* 根據接收的Excel文件來導入Excel,并封裝成實體類
*
* @param file 上傳的文件
* @param titleRows 表標題的行數
* @param headerRows 表頭行數
* @param pojoClass 映射的實體類
* @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;
}
/**
* 文件轉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("未解析到表格數據");
}
return list;
}
}excel導出所需要的都準備好了,下面來看一下我的效果:

到此這篇關于springboot利用easypoi實現簡單導出的文章就介紹到這了,更多相關springboot easypoi導出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring MVC如何使用@RequestParam注解獲取參數
這篇文章主要介紹了Spring MVC實現使用@RequestParam注解獲取參數方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
解決mybatis使用char類型字段查詢oracle數據庫時結果返回null問題
這篇文章主要介紹了mybatis使用char類型字段查詢oracle數據庫時結果返回null問題的解決方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06

