javascript
SpringBoot集成EasyPoi实现Excel导入导出
作者介紹:
本人Java特工,代號:Cris Li ; 中文名:克瑞斯理
簡書地址: 消失的碼農(nóng) - 簡書
CSDN地址: https://blog.csdn.net/jianli95
個人純潔版博客: https://lijian69.github.io/blog/
為什么要使用 EasyPoi
分析:當(dāng)下流行的Excel導(dǎo)出的Poi工具
| EasyPOI | 作者推薦:快,方便,集成性高 | http://easypoi.mydoc.io/ |
| EasyExcel | alibaba出品的,也是快,方便,但是在樣式方面 做的不夠好 | https://github.com/alibaba/easyexcel |
| POI | poi工具的老大哥,功能超級全,但是學(xué)習(xí)難度較高,代碼的數(shù)量較多 |
EasyPoi的詳細介紹
easypoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員
就可以方便的寫出Excel導(dǎo)出,Excel模板導(dǎo)出,Excel導(dǎo)入,Word模板導(dǎo)出,通過簡單的注解和模板
語言(熟悉的表達式語法),完成以前復(fù)雜的寫法
獨特的功能
- 基于注解的導(dǎo)入導(dǎo)出,修改注解就可以修改Excel
- 支持常用的樣式自定義
- 基于map可以靈活定義的表頭字段
- 支持一堆多的導(dǎo)出,導(dǎo)入
- 支持模板的導(dǎo)出,一些常見的標(biāo)簽,自定義標(biāo)簽
- 支持HTML/Excel轉(zhuǎn)換,如果模板還不能滿足用戶的變態(tài)需求,請用這個功能
- 支持word的導(dǎo)出,支持圖片,Excel
使用步驟
1.maven 或者 Gradle 引入相關(guān)依賴
<dependencies><!-- 集成easypoi組件 .導(dǎo)出excel http://easypoi.mydoc.io/ --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.2.0</version></dependency><!-- 這個依賴相當(dāng)于上面 easypoi-base、easypoi-web、easypoi-annotation這3個依賴,可以引入上面3個依賴,也可以引入下面這一個,兩個方案二選一--><!-- <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>3.2.0</version></dependency> --></dependencies>2.定義實體對象(也是 你下載的對象,這里只是簡單注解)
@ExcelTarget("20") @Data public class User implements java.io.Serializable{@Excel(name = "id", width=15)@NotBlank(message = "該字段不能為空")private Integer id;@Excel(name = "姓名", orderNum = "0", width=30)private String name;@Excel(name = "性別", replace = { "男_1", "女_2" }, orderNum = "1", width=30)private String sex;@Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2", width=30)private String birthday; }3. Excel導(dǎo)入導(dǎo)出工具類 、封裝了調(diào)用EasyPoi APi底層接口的Excel導(dǎo)入導(dǎo)出工具類,直接調(diào)用工具類即可
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 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.io.IOException; import java.net.URLEncoder; import java.util.List; import java.util.Map; import java.util.NoSuchElementException;//Excel導(dǎo)入導(dǎo)出工具類 public class ExcelUtils {public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,boolean isCreateHeader, HttpServletResponse response) {ExportParams exportParams = new ExportParams(title, sheetName);exportParams.setCreateHeadRows(isCreateHeader);defaultExport(list, pojoClass, fileName, response, exportParams);}public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,HttpServletResponse response) {defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));}public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {defaultExport(list, fileName, response);}private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,ExportParams exportParams) {Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);if (workbook != null);downLoadExcel(fileName, response, workbook);}private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {try {response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));workbook.write(response.getOutputStream());} catch (IOException e) {// throw new NormalException(e.getMessage());}}private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);if (workbook != null);downLoadExcel(fileName, response, workbook);}public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {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) {// throw new NormalException("模板不能為空");} catch (Exception e) {e.printStackTrace();// throw new NormalException(e.getMessage());}return list;}public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,Class<T> pojoClass) {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) {// throw new NormalException("excel文件不能為空");} catch (Exception e) {// throw new NormalException(e.getMessage());System.out.println(e.getMessage());}return list;}}4.導(dǎo)出即可
List<User> personList = userService.findAll(); // 導(dǎo)出操作 ExcelUtils.exportExcel(personList, "easypoi導(dǎo)出功能", "導(dǎo)出sheet1", User.class, "測試user.xls", re
作者:消失的碼農(nóng)
鏈接:https://www.jianshu.com/p/c91cd893663d
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot集成EasyPoi实现Excel导入导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 易贷在线不退款怎么办
- 下一篇: SpringBoot图文教程9—Spri