java读取excel-POI


做接口测试时,通常需要读取测试用例数据,apache早就为我们提供了读取excel数据的jar包-POI。

使用POI读取excel方法

(1)创建maven项目,添加poi依赖

创建maven项目,file --> new Project -->maven

添加依赖,pom.xml文件中添加。刷新maven。

    <dependencies>
        <!--读取 excel 的所需jar包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

 

 (2)创建TestCase类,用于将测试用例存储到对象中。

public class TestCase {
    /**
     * 测试用例对象
     */
    String CaseId;
    String Describe;
    String Url;
    String Method;
    String Parameters;
    String Expect;
    String Actual;

    public String getCaseId() {
        return CaseId;
    }

    public void setCaseId(String caseId) {
        CaseId = caseId;
    }

    public String getDescribe() {
        return Describe;
    }

    public void setDescribe(String describe) {
        Describe = describe;
    }

    public String getUrl() {
        return Url;
    }

    public void setUrl(String url) {
        Url = url;
    }

    public String getMethod() {
        return Method;
    }

    public void setMethod(String method) {
        Method = method;
    }

    public String getParameters() {
        return Parameters;
    }

    public void setParameters(String parameters) {
        Parameters = parameters;
    }

    public String getExpect() {
        return Expect;
    }

    public void setExpect(String expect) {
        Expect = expect;
    }

    public String getActual() {
        return Actual;
    }

    public void setActual(String actual) {
        Actual = actual;
    }

    @Override
    public String toString() {
        return "TestCase{" +
                "CaseId='" + CaseId + '\'' +
                ", Describe='" + Describe + '\'' +
                ", Url='" + Url + '\'' +
                ", Method='" + Method + '\'' +
                ", Parameters='" + Parameters + '\'' +
                ", Expect='" + Expect + '\'' +
                ", Actual='" + Actual + '\'' +
                '}';
    }
}

(3)读取excel,并将读取的数据存储到对象列表中。

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.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Excelread {
    public static void main(String[] args) {
        ArrayList<TestCase> list = new ArrayList<>();

        File file = new File("D:\\testjavaIO\\test11\\testcase.xlsx");
        String sheetName = "Sheet1";
        try {
            //XSSFWorkbook 是读取2007以上版本的表格,及.xlsx结尾,所以使用XSSFWorkbook
            //HSSFWorkbook 是读取2003版本的表格,及.xls结尾
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
            //获取表
            Sheet sheet = xssfWorkbook.getSheet(sheetName);
            //遍历表格
            //起始行,第一行是标题栏不需要读取
            int firstRowIndex = sheet.getFirstRowNum() + 1;
            //结束行
            int lastRowIndex = sheet.getLastRowNum();
            //遍历行
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
                Row row = sheet.getRow(rIndex);
                if(row != null){
                    //存储遍历的每行用例的元素值
                    String[] listcase = new String[row.getLastCellNum() - row.getFirstCellNum()];
                    //起始列
                    int firstCellIndex = row.getFirstCellNum();
                    //结束列
                    int lastCellIndex = row.getLastCellNum();
                    for (int cIndex = firstCellIndex; cIndex <= lastCellIndex; cIndex++) {
                        Cell cell = row.getCell(cIndex);
                        if(cell != null){
                            listcase[cIndex] = cell.toString();
                        }
                    }
                    //将每条测试用例存储到一个testcase对象
                    TestCase testCase = new TestCase();
                    testCase.setCaseId(listcase[0]);
                    testCase.setDescribe(listcase[1]);
                    testCase.setUrl(listcase[2]);
                    testCase.setMethod(listcase[3]);
                    testCase.setParameters(listcase[4]);
                    list.add(testCase);
                }
                
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        for(int i = 0; i < list.size();i++) {
            System.out.println(list.get(i).toString());
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM