poi操作excel-查询封装
来源:互联网 发布:wifi网络共享软件 编辑:程序博客网 时间:2024/05/18 22:18
1.学习自动化的第一步,操作poi的类,获取excle数据驱动并成功遍历
代码1,地址封装
package test.pom.api;
public class SourceAddress {
//测试案例地址
public static String filePath="C:/Users/yvon/Desktop/1.xlsx";
//测试案例sheet页名称
public static String sheetName="TestCase";
}
代码2,poi操作
package test.pom.api;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class TestCaseUtils {
public static void main(String[] args) {
excelShow(TestCaseUtils.excelRead());
}
/*
* 1.获取数据源
* 2.遍历数据
*
*
* */
public static void excelShow(Map<String, HashMap> map){
//Map<String, HashMap> map = TestCaseUtils.excelRead();
HashMap hashMap = map.get("1");
System.out.println("hashMap"+hashMap);
Set set = hashMap.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> entry = (Entry<Integer, String>) it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
/*1.获取file文件
*2.遍历excel案例
*3.每条案例的testCaseCode为key,字段和对应值为value,作为Map<String, HashMap>
*
* */
public static Map<String, HashMap> excelRead(){
File file = getFile(SourceAddress.filePath);
Workbook wb = null;
Sheet sh = null;
Map <String, HashMap> excelContents = new HashMap();
try {
wb = Workbook.getWorkbook(file);
sh = wb.getSheet(SourceAddress.sheetName);
//行数
int rowsCount = sh.getRows();
System.out.println("rowsCount:::::"+rowsCount);
//列数
int ColumnsCount = sh.getColumns();
System.out.println("ColumnsCount:::::::"+ColumnsCount);
//System.out.println(sh.getCell(1, 1).getContents().toString());
if(rowsCount<2){
System.out.println("无测试案例!");
}else{
System.out.println("有测试案例!");
//检查是否存在空行
//定义内容
String rowContents ="";
for(int i = 0;i < rowsCount;i++){
for(int j = 0;j < ColumnsCount;j++){
//取出具体列cell的值
//System.out.println(sh.getCell(j, i).getContents().toString());
rowContents = rowContents + sh.getCell(j, i).getContents().toString();
}
}
}
//System.out.println("testCaseCode111:::::"+sh.getCell(2,0).getContents().toString());
//读取内容
for(int rowIndex = 1;rowIndex < rowsCount;rowIndex++){
//获取testNo字段列值
//System.out.println("testCaseCode:::::"+sh.getCell(0,rowIndex).getContents().toString());
String testCaseCode = sh.getCell(0,rowIndex).getContents().toString();
HashMap<String, String> rowMap = new HashMap();
for(int colIndex = 0;colIndex < ColumnsCount;colIndex++){
//取出具体列cell的值
//System.out.println(sh.getCell(j, i).getContents().toString());
//rowContents = rowContents + sh.getCell(j, i).getContents().toString();
//取出字段值
//System.out.println("testCaseCode111:::::"+sh.getCell(colIndex,0).getContents().toString());
//取出实体值
//System.out.println("testCaseCode222:::::"+sh.getCell(colIndex,rowIndex).getContents().toString());
//取出实体值和字段值对应Map<key value>
rowMap.put(sh.getCell(colIndex,0).getContents().toString(), sh.getCell(colIndex,rowIndex).getContents().toString());
}
excelContents.put(testCaseCode, rowMap);
}
//return excelContents;
wb.close();
return excelContents;
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//获取文件
public static File getFile(String path){
File file = null;
try {
file = new File(path);
} catch (Exception e) {
// TODO: handle exception
}
return file;
}
}
- poi操作excel-查询封装
- POI操作EXCEL--封装类
- 新手封装的POI操作Excel
- 封装poi操作Excel工具类
- 基于POI的简单EXCEL操作封装
- 封装poi导出EXCEL
- 使用poi操作excel,循环查询数据库导出excel
- 自己封装的poi操作Excel工具类
- Apache POI封装工具类: 操作Excel表格
- poi读取excel封装例子
- Java POI Excel封装例子
- Java POI Excel封装例子
- poi封装导出Excel【java】
- POI解析Excel封装工具
- 简单封装POI导出excel
- POI操作EXCEL
- POI操作EXCEL
- POI操作Excel文档
- vue项目中如何使用字体图标
- think in java第十一章持有对象 习题答案
- Aspose.Cells 8月新版17.8发布 | 附下载
- RNN结构及反BPTT向传播算法
- 使用python爬取百度图片
- poi操作excel-查询封装
- 数据库ACID特性
- 计算机视觉笔记(三)图像处理(2)霍夫变换、角点检测、图像匹配SIFT
- flume kafka sparkstreaming整合后spark executor dead 及集群报错java.io.IOException: Connection reset by peer
- labview运行和停止vi
- 接口测试工作心得记录一
- 依赖注入框架Dagger2从入门到放弃
- 阿里云新一代关系型数据库 PolarDB 剖析
- Hbase笔记:使用hbaseAPI实现表的创建与循环数据插入