Easypoi实现excel多sheet表导入导出功能
Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写。
- 第一步引入Easypoi依赖
<!-- 导出文件工具 EasyPoi实现Excel读写管理测试用例 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
- Easypoi的注解使用说明(存留查看即可)

- 第二步定义对应表格头数据对象实体类(注解的使用可以查阅上面的按需使用即可)


@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(登录账号)",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(实际测试结果)",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(请求类型)",orderNum = "2",width = 20)
private String type;
@Excel(name = "url(访问地址)",orderNum = "3",width = 40)
private String url;
}
- 第三步:封装Easypoi工具类(网上查了很多但是并不完整,这里补充下)
参考文章
关键封装工具类多sheet导入方法
/**
* 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
* @param filePath 导入文件路径
* @param sheetIndex 导入sheet索引
* @param titleRows 表标题的行数
* @param headerRows 表头行数
* @param pojoClass Excel实体类
* @return
*/
public static <T> List<T> importExcel(String filePath,int sheetIndex,Integer titleRows, Integer headerRows, Class<T> pojoClass) {
// 根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
ImportParams params = new ImportParams();
// 第几个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导入示例(直接传入sheet索引获取对应的sheet表)

多sheet表导出方法使用(需要把导入的多sheet表数据转成list集合获取新数据后调用该方法重新写入)
/**
* 功能描述:把同一个表格多个sheet测试结果重新输出,如果后续增加多个List<Map<String, Object>>对象,需要后面继续追加
* @ExcelEntiry sheet表格映射的实体对象
* @return
*/
public static String exportSheet( Object...objects){
Workbook workBook = null;
try {
// 创建参数对象(用来设定excel得sheet得内容等信息)
ExportParams deptExportParams = new ExportParams();
// 设置sheet得名称
deptExportParams.setSheetName("登录用例");
// 设置sheet表头名称
deptExportParams.setTitle("测试用例");
// 创建sheet1使用得map
Map<String, Object> deptExportMap = new HashMap<>();
// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
deptExportMap.put("title", deptExportParams);
// 模版导出对应得实体类型
deptExportMap.put("entity", LoginCaseDto.class);
// sheet中要填充得数据
deptExportMap.put("data", objects[0]);
ExportParams empExportParams = new ExportParams();
empExportParams.setTitle("被测RUL路径");
empExportParams.setSheetName("被测url");
// 创建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进行包装
List<Map<String, Object>> sheetsList = new ArrayList<>();
sheetsList.add(deptExportMap);
sheetsList.add(empExportMap);
// 执行方法
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";
}
最后即可获取新的测试结果表格。
项目源码地址传送门
更多测试技术分享、学习资源以及一些其他福利可关注公众号:【Coding测试】获取:

Easypoi实现excel多sheet表导入导出功能的更多相关文章
- Laravel Excel 实现 Excel-CSV 文件导入导出功能
Laravel Excel 是一款基于 PHPExcel 开发的Laravel框架专用的 Excel/CSV 文件导入导出功能的扩展包,用起来的非常方便. 它的 Github 地址是:https:// ...
- poi实现excel的导入导出功能
Java使用poi实现excel的导入导出功能: 工具类ExcelUtil,用于解析和初始化excel的数据:代码如下 package com.raycloud.kmmp.item.service.u ...
- SpringCloud微服务实战——搭建企业级开发框架(三十):整合EasyExcel实现数据表格导入导出功能
批量上传数据导入.数据统计分析导出,已经基本是系统必不可缺的一项功能,这里从性能和易用性方面考虑,集成EasyExcel.EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项 ...
- oracle导入导出功能
1.普通版:oracle导入导出功能:导出exp 用户名/密码@SID file=f:\xx.dmp owner=用户名 导入imp 用户名/密码@SID full=y file=f:\xx.dmp ...
- vue+element-ui的简洁导入导出功能
1.前段后台管理系统中数据展示一般都是用表格,表格会涉及到导入和导出;原生js导出excel2.导入是利用element-ui的Upload 上传组件; <el-upload class=&qu ...
- vue + element-ui实现简洁的导入导出功能
1.安装ElementUI模块 cnpm install element-ui -S 2.在main.js中引入 import ElementUI from 'element-ui' import ' ...
- office在繁体系统下 导入导出 功能灰显的解决方法
当在win7系统使用繁体中文版的office时,或系统是繁体版时,可能会导致office的导入导出功能无法使用 解决方法: 控制面板--区域和语言--格式--中文简体
- Asp.net的对Excel文档的导入导出操作
刚刚初入职场,在休闲的时间写下了项目中用到的对Excel文档操作的方法以及总结,多的不说,直接上代码 public static void CreateExcel(DataSet ds, string ...
- 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件
今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...
随机推荐
- php Zookeeper使用踩坑
用的是Zookeeper扩展,Php版本为7.2.17,下载地址: https://pecl.php.net/package/zookeeper 用的是0.6.4版本 创建节点官方给的示例如下: &l ...
- SpringBoot框架:'url' attribute is not specified and no embedded datasource could be configured问题处理
一.问题如下: Description: Failed to configure a DataSource: 'url' attribute is not specified and no em ...
- web自动化整理
HTML学习 https://www.w3school.com.cn/html/html_headings.asp 如何写html呢? 可以选择软件 文本编辑器sublime(http://www.s ...
- mysql-10-union
#进阶10:联合查询 /* union联合 将多条查询语句的结果合并成一个结果 语法: 查询1 union 查询2 union 查询3 ... 应用场景:要查询的结果来自于多个表,且多个表没有直接的连 ...
- 015 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 09 Unicode编码
015 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 09 Unicode编码 本文知识点:Unicode编码以及字符如何表示? ASCII码是美国提出的标准信息 ...
- c中_tmain()和main()区别
来源参考:https://www.cnblogs.com/lucyjiayou/archive/2011/05/07/2039621.html tchar.h>可以找到,如#define _tm ...
- Java之微信支付(扫码支付模式二)案例实战
摘要:最近的一个项目中涉及到了支付业务,其中用到了微信支付和支付宝支付,在做的过程中也遇到些问题,所以现在总结梳理一下,分享给有需要的人,也为自己以后回顾留个思路. 一:微信支付接入准备工作: 首先, ...
- 一道web入门题
9月27日00:00 这道题是我将hctf_warmup魔改之后得到的,难度比较低,主要还是讲一些web相关的思考方式,所以这篇文章会比较冗长过于详细.(毕竟是给小姑娘入门看的23333).就当M1s ...
- ANNdotNET中的视觉神经网络设计器
ANNdotNET简介 是一个在.net平台上进行深度学习的开源项目.NET Framework和.NET Core).该项目位于http://github.com/bhrnjica/anndotne ...
- ASP。NET Core Blazor CRUD使用实体框架和Web API
下载source code - 1.7 MB 介绍 *请查看我的Youtube视频链接来学习ASP.NET Core Blazor CRUD使用实体框架和Web API. 在本文中,我们将了解如何为A ...