1. 程式人生 > >easypoi 讀取 Excel 簡單應用

easypoi 讀取 Excel 簡單應用

# 背景 在做介面測試的時候,經常會使用 Excel 來儲存對應的介面資訊和用例資訊,為了方便程式的讀取,引入easypoi 工具來讀取 Excel 內容。easypoi 比起 poi 使用更加的方便,程式碼量也少很多。 # 應用程式碼 例如我想讀取下面 Excel 中的介面資訊: ![](https://javage.github.io/images/blog/介面引數.png) - 引入對應 easypoi 依賴: ```xml cn.afterturn easypoi-annotation 4.0.0 cn.afterturn easypoi-base 4.0.0 org.hibernate hibernate-validator 5.2.4.Final javax.el javax.el-api 2.2.4 ``` - 建立對應的實體類: ```java package com.ggf.juhe.pojo; import cn.afterturn.easypoi.excel.annotation.Excel; import javax.validation.constraints.NotNull; /** * @Description: 介面資訊實體類 * @Author: ggf * @Date: 2020/06/13 */ public class ApiInfo { /** * 介面編號 */ @Excel(name="介面編號") private String id; /** * 介面名稱 */ @Excel(name="介面名稱") private String name; /** * 請求URL */ @Excel(name="介面地址") @NotNull private String url; /** * 介面請求方式 */ @Excel(name="介面提交方式") private String method; /** * 請求資料型別 */ @Excel(name="介面引數型別") private String type; public ApiInfo() { } public ApiInfo(String id, String name, String method, String url, String type) { this.id = id; this.name = name; this.method = method; this.url = url; this.type = type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "ApiInfo{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", method='" + method + '\'' + ", url='" + url + '\'' + ", type='" + type + '\'' + '}'; } } ``` - easypoi 讀取 Excel 程式碼: ```java package com.ggf.juhe.demo; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; import com.ggf.juhe.pojo.ApiInfo; import java.io.FileInputStream; import java.io.IOException; import java.util.Collections; import java.util.List; /** * @Description: * @Author: ggf * @Date: 2020/06/13 */ public class EasypoiDemo { public static void main(String[] args) { String filePath = "src/main/resources/jhapi_case.xlsx"; List apiInfos = readExcel(filePath, 0, ApiInfo.class); for (ApiInfo apiInfo : apiInfos) { System.out.println(apiInfo); } } /** * * @param filePath Excel檔案路徑 * @param sheetIndex 對應的表單,從0開始,0代表第一個表單 * @param clazz 對應封裝的資料例項物件 * @return 返回資料集合 */ public static List readExcel(String filePath, int sheetIndex, Class clazz) { // 定義輸入流 FileInputStream fis = null; List datas = null; try { // 建立輸入流物件 fis = new FileInputStream(filePath); // 建立一個easypoi使用的配置類 ImportParams params = new ImportParams(); // 設定表格座標 params.setStartSheetIndex(sheetIndex); // 校驗Excel檔案,去掉空行 params.setNeedVerify(true); // 讀取資料 datas = ExcelImportUtil.importExcel(fis, clazz, params); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return datas; } } ``` - 列印結果 ![](https://javage.github.io/images/blog/列印內