博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java解析Excel
阅读量:4347 次
发布时间:2019-06-07

本文共 4162 字,大约阅读时间需要 13 分钟。

前两天总结了些关于Excel和CSV结合TestNG进行数据驱动测试的例子,对于Excel存放TestCase和关键字如何进行解析,也做了对应的总结,希望在学习的路上勇往直前,有不对的地方,希望大家指出,共同学习共同进步。

采用的是POI对Excel进行的解析,需要的Jar包文件:

poi-3.11-20141221.jar

poi-ooxml-3.11-20141221.jar

poi-ooxml-schemas-3.11-20141221.jar

xmlbeans-2.6.0.jar

代码如下:

 

package com.util.datadriver;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;/** * 通过POI对Excel中的数据进行基本的操作 *  * 需要导入额jar包: poi-3.11-20141221.jar、 poi-ooxml-3.11-20141221.jar、 *         poi-ooxml-schemas-3.11-20141221.jar、 xmlbeans-2.6.0.jar *  */public class ExcelController {    Workbook workbook = null;    Sheet sheet = null;    public void setExcelFile(String filePath, String fileName, String sheetName) {        try {            FileInputStream fis = new FileInputStream(new File(filePath));            String type = fileName.substring(fileName.indexOf("."));            if (type.equals(".xlsx")) {                workbook = new XSSFWorkbook(fis);            } else if (type.equals(".xls")) {                workbook = new HSSFWorkbook(fis);            }            sheet = workbook.getSheet(sheetName);            fis.close();        } catch (Exception e) {            e.printStackTrace();        }            }    //获取单元格的值    public String getCellData(int row, int column){                try {                        //获得Cell单元格对象            Cell cell = sheet.getRow(row).getCell(column);            //设置cell返回值的类型            cell.setCellType(Cell.CELL_TYPE_STRING);            //获取到cell单元格中的值            String cellData = cell.getStringCellValue();            return cellData;                    } catch (Exception e) {                        throw(e);        }    }        //设置单元格的值    @SuppressWarnings("static-access")    public void setCellData(String value, int rowCount, int columnCount, String filePath){                try {                        Row row = sheet.getRow(rowCount);            Cell cell = row.getCell(columnCount,row.RETURN_BLANK_AS_NULL);                        if(cell == null){                                row.createCell(columnCount).setCellValue(value);                            }else {                                cell.setCellValue(value);            }                        FileOutputStream fos = new FileOutputStream(new File(filePath));            workbook.write(fos);            fos.flush();            fos.close();                    } catch (Exception e) {                        e.printStackTrace();        }            }        //获取Excel的行数    public int getColCount(Sheet sheet1){                int firstColCount = sheet1.getFirstRowNum();        int lastColCount = sheet1.getLastRowNum();        int sumColCount = lastColCount - firstColCount + 1;        return sumColCount;            }        //获取Excel中每行的数据,并用数组返回每行所有数据,方便与TestNG做数据驱动        public Object[][] getExcelData(String filePath, String fileName,            String sheetName) throws Exception {        int sumRowCount = getColCount(sheet);;        List
list = new ArrayList
(); // 获取每行的行对象,第一行为信息栏,不计入,所以从1开始 for (int i = 1; i < sumRowCount; i++) { Row row = sheet.getRow(i); // 获得一行中最后单元格的count int lastCellCount = row.getLastCellNum(); // 定义一个数组来存放cell中值,根据cell的长度来定义数组的长度 String[] fileds = new String[lastCellCount]; for (int j = 0; j < lastCellCount; j++) { String cellValue = row.getCell(j).getStringCellValue(); fileds[j] = cellValue; } list.add(fileds); } // 定义一个object[][] 的二维数组,存放list中的值 Object[][] results = new Object[list.size()][]; // 设置二维数组每行的值, for (int a = 0; a < list.size(); a++) { results[a] = list.get(a); } return results; } }

转载于:https://www.cnblogs.com/taoSir/p/5106352.html

你可能感兴趣的文章
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
一般处理程序在VS2012中打开问题
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>