easypoi教程_easypoi 读取 Excel 简单应用

发布于:2023-04-27 ⋅ 阅读:(4342) ⋅ 点赞:(4)

背景

在做接口测试的时候,经常会使用 Excel 来存储对应的接口信息和用例信息,为了方便程序的读取,引入easypoi 工具来读取 Excel 内容。easypoi 比起 poi 使用更加的方便,代码量也少很多。

应用代码

例如我想读取下面 Excel 中的接口信息:

引入对应 easypoi 依赖:

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

创建对应的实体类:

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 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;

}

}

打印结果


网站公告

今日签到

点亮在社区的每一天
去签到