读取所有数据,并打印出来,表单名:testcase

java操作excel(通过POI)

 

定义实体类

package com.qzcsbj;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class TestCase {
    private String caseId;
    private String describe;
    private String url;
    private String method;
    private String parameters;
    private String expect;
    private String actual;

    public String getCaseId() {
        return caseId;
    }

    public void setCaseId(String caseId) {
        this.caseId = caseId;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public String getExpect() {
        return expect;
    }

    public void setExpect(String expect) {
        this.expect = expect;
    }

    public String getActual() {
        return actual;
    }

    public void setActual(String actual) {
        this.actual = actual;
    }

    @Override
    public String toString() {
        return "TestCase{" +
                "caseId='" + caseId + '\'' +
                ", describe='" + describe + '\'' +
                ", url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", parameters='" + parameters + '\'' +
                ", expect='" + expect + '\'' +
                ", actual='" + actual + '\'' +
                '}';
    }
}

 

读取excel

package com.qzcsbj;

import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class Test {
    public static void readExcel(String excelPath, String sheetName){
        InputStream in = null;
        try {
            File file = new File(excelPath);
            in = new FileInputStream(file);
            Workbook workbook = WorkbookFactory.create(in);
            Sheet sheet = workbook.getSheet(sheetName);
            Row firstRow = sheet.getRow(0);
            int lastCellNum = firstRow.getLastCellNum();
            String[] titles = new String[lastCellNum];
            for (int i = 0; i < lastCellNum; i++) {
                Cell cell = firstRow.getCell(i);
                String title = cell.getStringCellValue();
                titles[i] = title;
            }
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum  ; i++) {
                Row rowData = sheet.getRow(i);
                System.out.print("第"+i+"行数据:");
                for (int j = 0; j < lastCellNum ; j++) {
                    Cell cell = rowData.getCell(j);
                    String cellValue = cell.getStringCellValue();
                    // 打印获取到的值
                    System.out.print("【"+ titles[j] + "="+ cellValue+"】");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) {
        readExcel("E:\\case.xlsx","testcase");
    }
}

 

结果:

java操作excel(通过POI)

 

 

【bak】https://www.cnblogs.com/uncleyong/p/15861208.html

原文:https://www.cnblogs.com/uncleyong/p/15867741.html

 

相关文章: