Java使用easypoi快速導(dǎo)入導(dǎo)出的實(shí)現(xiàn)
簡(jiǎn)介
easypoi功能如同名字easy,主打的功能就是容易,讓一個(gè)沒(méi)見(jiàn)接觸過(guò)poi的人員就可以方便的寫(xiě)出Excel導(dǎo)入,導(dǎo)出,通過(guò)簡(jiǎn)單的注解和模板語(yǔ)言(熟悉的表達(dá)式語(yǔ)法),完成以前復(fù)雜的寫(xiě)法。
集成
pom 中引入依賴(lài)即可
<!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>
整合工具類(lèi) EasyPoiUtil
package cn.common.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 cn.common.exception.ZXException; 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; /** * @author huangy * @date 2019/6/28 14:57 */ public class EasyPoiUtil { /** * 導(dǎo)出Excel,包括文件名以及表名。是否創(chuàng)建表頭 * * @param list 導(dǎo)出的實(shí)體類(lèi) * @param title 表頭名稱(chēng) * @param sheetName sheet表名 * @param pojoClass 映射的實(shí)體類(lèi) * @param isCreateHeader 是否創(chuàng)建表頭 * @param fileName 文件名 * @param response * @return */ 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); } /** * 導(dǎo)出Excel 默認(rèn)格式 默認(rèn)有創(chuàng)建表頭 */ 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)); } /** * map多sheet形式導(dǎo)出 * @param list * @param fileName * @param response */ public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){ defaultExport(list, fileName, response); } /** * 常規(guī)默認(rèn)導(dǎo)出方式 * @param list * @param pojoClass * @param fileName * @param response * @param exportParams */ private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list); ExcelExportUtil.closeExportBigExcel(); if (workbook != null); downLoadExcel(fileName, response, workbook); } /** * 多sheet默認(rèn)導(dǎo)出方式 * @param list * @param fileName * @param response */ private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); ExcelExportUtil.closeExportBigExcel(); if (workbook != null); downLoadExcel(fileName, response, workbook); } /** * 下載excel * @param fileName * @param response * @param 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 ZXException(e.getMessage()); } } /** * 導(dǎo)入 文件路徑形式 * @param filePath * @param titleRows * @param headerRows * @param pojoClass * @param <T> * @return */ 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 ZXException("模板不能為空"); } catch (Exception e) { e.printStackTrace(); throw new ZXException(e.getMessage()); } return list; } /** * 導(dǎo)入 MultipartFile 形式 * @param file * @param titleRows * @param headerRows * @param pojoClass * @param <T> * @return */ 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 ZXException("excel文件不能為空"); } catch (Exception e) { throw new ZXException(e.getMessage()); } return list; } }
使用示例
實(shí)體類(lèi)
public class BlackListExport { @Excel(name = "客戶(hù)姓名", width = 15, orderNum = "2") private String name; @Excel(name = "備注", width = 10, orderNum = "1") private String remark; @Excel(name = "手機(jī)號(hào)", width = 15, orderNum = "0") private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public BlackListExport() { } public BlackListExport(String name, String remark, String phone) { this.name = name; this.remark = remark; this.phone = phone; } }
接口
@ApiOperation(value = "easyPoiUtil 導(dǎo)出測(cè)試") @GetMapping(value = "/poi/export1") public void export1(HttpServletResponse response){ List<BlackListExport> list=new ArrayList<>(); for(int i=0;i<10000;i++){ list.add(new BlackListExport(i+"",i+"",i+"")); } EasyPoiUtil.exportExcel(list,"zx","huangy",BlackListExport.class,"zx.xls",response); } /** * 如果填充不同sheet得data數(shù)據(jù)列表使用相同得list對(duì)象進(jìn)行填充的話(huà), * 會(huì)出現(xiàn)第一次填充得sheet有數(shù)據(jù),后續(xù)其他使用相同list對(duì)象進(jìn)行data填充得sheet沒(méi)有數(shù)據(jù)展示。 * @param response */ @ApiOperation(value = "多sheet 導(dǎo)出測(cè)試") @GetMapping(value = "/poi/export2") public void export2(HttpServletResponse response){ // 查詢(xún)數(shù)據(jù),此處省略 List list = new ArrayList<>(); list.add(new BlackListExport("姓名1","備注1","手機(jī)1")) ; list.add(new BlackListExport("姓名2","備注2","手機(jī)2")) ; list.add(new BlackListExport("姓名3","備注3","手機(jī)3")) ; List list2 = new ArrayList<>(); list2.add(new BlackListExport("姓名-1","備注-1","手機(jī)-1")) ; list2.add(new BlackListExport("姓名-2","備注-2","手機(jī)-2")) ; list2.add(new BlackListExport("姓名-3","備注-3","手機(jī)-3")) ; List<Map<String,Object>> sheetsList = new ArrayList<>(); for(int i=1;i<=4;i++){ // 設(shè)置導(dǎo)出配置 // 創(chuàng)建參數(shù)對(duì)象(用來(lái)設(shè)定excel得sheet得內(nèi)容等信息) ExportParams params = new ExportParams() ; // 設(shè)置sheet得名稱(chēng) params.setSheetName("表格"+i); //創(chuàng)建sheet使用的map Map<String,Object> dataMap = new HashMap<>(); // title的參數(shù)為ExportParams類(lèi)型,目前僅僅在ExportParams中設(shè)置了sheetName dataMap.put("title",params) ; // 模版導(dǎo)出對(duì)應(yīng)得實(shí)體類(lèi)型 dataMap.put("entity",BlackListExport.class) ; // sheet中要填充得數(shù)據(jù) if(i%2==0){ dataMap.put("data",list) ; }else { dataMap.put("data",list2) ; } sheetsList.add(dataMap); } EasyPoiUtil.exportExcel(sheetsList,"hy.xls",response); }
到此這篇關(guān)于Java使用easypoi快速導(dǎo)入導(dǎo)出的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java easypoi導(dǎo)入導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目打成War布署在Tomcat的詳細(xì)步驟
這篇文章主要介紹了SpringBoot項(xiàng)目打成War布署在Tomcat,本文分步驟結(jié)合圖文實(shí)例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03SpringBoot創(chuàng)建監(jiān)聽(tīng)器的方法示例
在Java中,監(jiān)聽(tīng)器(Listener)是一種設(shè)計(jì)模式,它允許對(duì)象在 特定事件 發(fā)生時(shí) 自動(dòng)執(zhí)行某些操作,這種設(shè)計(jì)模式通常用于實(shí)現(xiàn) 發(fā)布-訂閱模型,本文給大家介紹了SpringBoot創(chuàng)建監(jiān)聽(tīng)器的方法示例,感興趣的通過(guò)可以參考一下2024-04-04Java split函數(shù)拆分后變成null問(wèn)題解決方案
這篇文章主要介紹了Java split函數(shù)拆分后變成null問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10SpringBoot解決BigDecimal傳到前端后精度丟失問(wèn)題
這篇文章將通過(guò)示例詳細(xì)為大家介紹SpringBoot如何解決BigDecimal傳到前端后精度丟失問(wèn)題,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-06-06Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過(guò)程
幾十萬(wàn)上百萬(wàn)行的數(shù)據(jù)是很常見(jiàn)的。本文主要介紹了Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java使用continue語(yǔ)句的實(shí)例詳解
這篇文章主要介紹了Java使用continue語(yǔ)句的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握使用方法,需要的朋友可以參考下2017-10-10Java使用Hutool執(zhí)行日期的加法和減法操作方法
使用Hutool進(jìn)行日期的加法和減法操作,可以使用`DateUtil.offsetXXX()`方法來(lái)實(shí)現(xiàn),這些方法會(huì)返回一個(gè)新的日期,而不是在原日期上進(jìn)行修改,本文給大家介紹Java使用Hutool執(zhí)行日期的加法和減法操作方法,感興趣的朋友一起看看吧2023-11-11SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作
這篇文章主要介紹了SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06Spring boot詳解緩存redis實(shí)現(xiàn)定時(shí)過(guò)期方法
本篇文章分享的就是spring boot中的一個(gè)輪子,spring cache注解的方式實(shí)現(xiàn)接口數(shù)據(jù)緩存。默認(rèn)的配置想非常簡(jiǎn)單,但是有一個(gè)弊端是緩存數(shù)據(jù)為永久緩存,本次將介紹如何設(shè)置接口緩存數(shù)據(jù)的過(guò)期時(shí)間2022-07-07