背景
在做接口測試的時候,經常會使用 Excel 來存儲對應的接口信息和用例信息,為了方便程序的讀取,引入easypoi 工具來讀取 Excel 內容。easypoi 比起 poi 使用更加的方便,代碼量也少很多。
應用代碼
例如我想讀取下面 Excel 中的接口信息:
- 引入對應 easypoi 依賴:
<!--easypoi依賴-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
</dependency>
<!-- 添加校驗,在easypoi讀取Excel時,可以去掉空行,同時對字段進行強制校驗 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
- 創建對應的實體類:
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 代碼:
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<ApiInfo> 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 <E> List<E> readExcel(String filePath, int sheetIndex, Class<E> clazz) {
// 定義輸入流
FileInputStream fis = null;
List<E> 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;
}
}
- 打印結果