您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在Java中利用Easypoi導(dǎo)出excel內(nèi)容,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
第一步引入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(請(qǐng)求類型)",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"; }
上述就是小編為大家分享的怎么在Java中利用Easypoi導(dǎo)出excel內(nèi)容了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。