背景

在做接口测试的时候,经常会使用 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;
}
}
  • 打印结果

easypoi 读取 Excel 简单应用的更多相关文章

  1. 第四篇:java读取Excel简单模板

    场景:对于经常需要导入Excel模板或数据来解析后加以应用的,使用频率非常之高,做了一个比较稳定的版本,体现在这些地方工具:org.apache.poi使用前必须了解这些:1.要解析,那肯定先判断是不 ...

  2. Java web的读取Excel简单Demo

    目录结构: Data.xls数据:   后台页面: GetExcelData.java       public void doGet(HttpServletRequest request, Http ...

  3. 简单poi读取excel

    1.添加依赖jar包 maven配置: <!-- poi being --> <dependency> <groupId>org.apache.poi</gr ...

  4. JAVA使用POI读取EXCEL文件的简单model

    一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...

  5. Unity用Excel.dll简单读取Excel内容

    Unity用Excel.dll简单读取Excel内容 需要Excel.dll 需要如下三个命名空间 using System.IO; using Excel; using System.Data; 1 ...

  6. C#读取Excel文件的简单方法

    一.简述 本文讲C#通过第三方库读取Excel的最简单的方法,下文给一个读取行数的例子. 二.依赖 引入nuget.org包如下: <?xml version="1.0" e ...

  7. python pandas模块简单使用(读取excel为例)

    第一步:模块安装 pip install pandas 第二步:使用(单个工作表为例) 说明:如果有多个工作表,那么只要指定sheetname=索引,(第一个工作表为0,第二个工作表为1,以此类推) ...

  8. Vue+EasyPOI导出Excel(带图片)

    一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...

  9. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

随机推荐

  1. E. Physical Education Lessons 动态开辟线段树区间更新

    E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...

  2. 【Mac】屏蔽系统升级更新

    三行代码解决Mac升级弹窗,小红点数字1

  3. 谈谈对ThreadLocal类的理解

    源码中对于ThreadLocal类的解释是: /** * This class provides thread-local variables. These variables differ from ...

  4. 人机协同与AI能力训练

    我们进行<中台战略>一书的第三期分享. “人机融合是解决aI机器人冷启动的绝佳解决方案,我们这里引入了一个应答满意度的指标,每一个咨询应答都对应一个应答满意度.当消费者应该回答选择转入人工 ...

  5. 【转】最长公共子序列(LCS),求LCS长度和打印输出LCS

    求LCS的长度,Java版本: public static int LCS(int[]a,int[] b) { int [][]c=new int[a.length+1][b.length+1]; f ...

  6. List<T> 的扩展方法

    //List<T>.Take(m)      //取出 前m行 IEnumerable<Person> takeList = lstPerson.Take(4); foreac ...

  7. php mysqli使用

    连接到数据库$mysqli = new mysqli(主机,用户,密码,数据库); 选择数据库$mysqli->select_db(数据库);设置编码$mysqli->set_charse ...

  8. css 盒模型、box-sizing 学习笔记

    默认情况下,给元素设置的高度和宽度是元素内容区的宽度和高度,给元素加padding 和 border ,元素的实际宽度和高度的计算方式是下面的两个公式: 元素的宽度= 元素的内容区宽度 + 内边距宽度 ...

  9. 05 . Python入门值循环语句

    一.Python循环语句 程序一般情况下是按照顺序执行的 编程语言提供了各种控制结构,允许更复杂的执行路径 Python中的循环语句有for和while但没有do while 循环语句允许我们执行一个 ...

  10. PIX防火墙配置A/S故障切换

    PIX防火墙配置A/S故障切换 1.基本命令 failover show failover failover lan enable failover lan interface zwish e2 fa ...