Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能
Easypoi簡化了開發(fā)中對(duì)文檔的導(dǎo)入導(dǎo)出實(shí)現(xiàn),并不像poi那樣都要寫大段工具類來搞定文檔的讀寫。
第一步引入Easypoi依賴
<!-- 導(dǎo)出文件工具 EasyPoi實(shí)現(xiàn)Excel讀寫管理測試用例 --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.2.0</version> </dependency>
Easypoi的注解使用說明(存留查看即可)
第二步定義對(duì)應(yīng)表格頭數(shù)據(jù)對(duì)象實(shí)體類(注解的使用可以查閱上面的按需使用即可)
@Setter @Getter @ToString public class LoginCaseDto { @Excel(name = "flag(0是反向,1是正向)",orderNum = "1",width = 20) private String flag; @Excel(name = "urlid(訪問id)",orderNum = "2",width = 20) private String urlid; @Excel(name = "name(登錄賬號(hào))",orderNum = "3",width = 20) private String name; @Excel(name = "pwd(登錄密碼)",orderNum = "4",width = 20) private String pwd; @Excel(name = "desc(期望提示語)",orderNum = "5",width = 40) private String desc; @Excel(name = "actual(實(shí)際測試結(jié)果)",orderNum = "6",width = 40 ) private String actual; @Excel(name = "urlpath(被測路徑)",orderNum = "7",width = 40 ) private String urlpath; }
public class LoginUrlDto { @Excel(name = "id(訪問測試類型)",orderNum = "1",width = 20) private String id; @Excel(name = "type(請求類型)",orderNum = "2",width = 20) private String type; @Excel(name = "url(訪問地址)",orderNum = "3",width = 40) private String url; }
第三步:封裝Easypoi工具類(網(wǎng)上查了很多但是并不完整,這里補(bǔ)充下)
參考文章
關(guān)鍵封裝工具類多sheet導(dǎo)入方法
/** * 功能描述:根據(jù)接收的Excel文件來導(dǎo)入多個(gè)sheet,根據(jù)索引可返回一個(gè)集合 * @param filePath 導(dǎo)入文件路徑 * @param sheetIndex 導(dǎo)入sheet索引 * @param titleRows 表標(biāo)題的行數(shù) * @param headerRows 表頭行數(shù) * @param pojoClass Excel實(shí)體類 * @return */ public static <T> List<T> importExcel(String filePath,int sheetIndex,Integer titleRows, Integer headerRows, Class<T> pojoClass) { // 根據(jù)file得到Workbook,主要是要根據(jù)這個(gè)對(duì)象獲取,傳過來的excel有幾個(gè)sheet頁 ImportParams params = new ImportParams(); // 第幾個(gè)sheet頁 params.setStartSheetIndex(sheetIndex); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); } catch (NoSuchElementException e) { throw new RuntimeException("模板不能為空"); } catch (Exception e) { e.printStackTrace(); } return list; }
excel導(dǎo)入示例(直接傳入sheet索引獲取對(duì)應(yīng)的sheet表)
多sheet表導(dǎo)出方法使用(需要把導(dǎo)入的多sheet表數(shù)據(jù)轉(zhuǎn)成list集合獲取新數(shù)據(jù)后調(diào)用該方法重新寫入)
/** * 功能描述:把同一個(gè)表格多個(gè)sheet測試結(jié)果重新輸出,如果后續(xù)增加多個(gè)List<Map<String, Object>>對(duì)象,需要后面繼續(xù)追加 * @ExcelEntiry sheet表格映射的實(shí)體對(duì)象 * @return */ public static String exportSheet( Object...objects){ Workbook workBook = null; try { // 創(chuàng)建參數(shù)對(duì)象(用來設(shè)定excel得sheet得內(nèi)容等信息) ExportParams deptExportParams = new ExportParams(); // 設(shè)置sheet得名稱 deptExportParams.setSheetName("登錄用例"); // 設(shè)置sheet表頭名稱 deptExportParams.setTitle("測試用例"); // 創(chuàng)建sheet1使用得map Map<String, Object> deptExportMap = new HashMap<>(); // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設(shè)置了sheetName deptExportMap.put("title", deptExportParams); // 模版導(dǎo)出對(duì)應(yīng)得實(shí)體類型 deptExportMap.put("entity", LoginCaseDto.class); // sheet中要填充得數(shù)據(jù) deptExportMap.put("data", objects[0]); ExportParams empExportParams = new ExportParams(); empExportParams.setTitle("被測RUL路徑"); empExportParams.setSheetName("被測url"); // 創(chuàng)建sheet2使用得map Map<String, Object> empExportMap = new HashMap<>(); empExportMap.put("title", empExportParams); empExportMap.put("entity", LoginUrlDto.class); empExportMap.put("data", objects[1]); // 將sheet1、sheet2使用得map進(jìn)行包裝 List<Map<String, Object>> sheetsList = new ArrayList<>(); sheetsList.add(deptExportMap); sheetsList.add(empExportMap); // 執(zhí)行方法 workBook = EasyPoiUtil.exportExcel(sheetsList, ExcelType.HSSF); //String fileName = URLEncoder.encode("test", "UTF-8"); String filepath = (String) LoadStaticConfigUtil.getCommonYml( "testcaseexcel.cases"); FileOutputStream fos = new FileOutputStream(filepath); workBook.write(fos); fos.close(); }catch (Exception e){ e.printStackTrace(); }finally { if(workBook != null) { try { workBook.close(); } catch (IOException e) { e.printStackTrace(); } } } return "success"; }
最后即可獲取新的測試結(jié)果表格。
項(xiàng)目源碼地址傳送門
到此這篇關(guān)于Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能的文章就介紹到這了,更多相關(guān)Easypoi excel多sheet表導(dǎo)入導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出
- Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出數(shù)據(jù)庫的方法示例
- java實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- java使用EasyExcel導(dǎo)入導(dǎo)出excel
- Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出操作詳解
- java利用easyexcel實(shí)現(xiàn)導(dǎo)入與導(dǎo)出功能
- java操作excel導(dǎo)入導(dǎo)出的3種方式
- Java使用EasyExcel實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- Java如何使用poi導(dǎo)入導(dǎo)出excel工具類
- java如何在項(xiàng)目中實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出功能
相關(guān)文章
spring?Cloud微服務(wù)阿里開源TTL身份信息的線程間復(fù)用
這篇文章主要為大家介紹了spring?Cloud微服務(wù)中使用阿里開源TTL身份信息的線程間復(fù)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01JAVA把結(jié)果保留兩位小數(shù)的3種方法舉例
在寫程序的時(shí)候,有時(shí)候可能需要設(shè)置小數(shù)的位數(shù),所以下面這篇文章主要給大家介紹了關(guān)于JAVA把結(jié)果保留兩位小數(shù)的3種方法,文章通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08實(shí)例講解Java的Spring框架中的控制反轉(zhuǎn)和依賴注入
這篇文章主要介紹了Java的Spring框架中的控制反轉(zhuǎn)和依賴注入,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-02-02Java語言實(shí)現(xiàn)反轉(zhuǎn)鏈表代碼示例
這篇文章主要介紹了Java語言實(shí)現(xiàn)反轉(zhuǎn)鏈表代碼示例,小編覺得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10