如何使用POM Maven项目中的数据提供程序从Excel工作表中读取数据

时间:2018-07-26 06:57:48

标签: java excel testng-dataprovider

我无法使用数据提供程序从excel读取数据。 我正在创建一个类以从excel读取数据,但出现错误。 [附屏幕截图] 现在如何在Test类中使用它们来调用它们。

public class DataProvider extends Base {
    // ExcelApiTest eat = null;
    //String xlfilePath = "";
    //String SheetName = "";


    @DataProvider(name = "UserData")
    public Object[][] userFormData() throws Exception {
        Object[][] data = testData(xlfilePath, SheetName);
        return data;
    }


    public Object[][] testData(String xlfilePath, String SheetName) throws Exception {
        Object[][] excelData = null;
        eat = new ExcelApiTest(xlfilePath);

        int rows = eat.getRowCount(SheetName);
        int columns = eat.getColumnCount(SheetName);

        excelData = new Object[rows - 1][columns];

        for (int i = 1; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                excelData[i - 1][j] = eat.getcellData(SheetName, j, i);
            }
        }
        return excelData;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以将以下代码用于用户名和密码,并对其进行修改以用于更多的列或字段:

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRedaer {

     /**
     * @param filePath  excel file path
     * @param sheetName  sheet name in xlsx file
     * @return excel data
     * @throws InvalidFormatException
     * @throws IOException
     */
    public static Object[][] readExcel(String filePath, String sheetName) throws InvalidFormatException, IOException {
            FileInputStream file= new FileInputStream(filePath);
            XSSFWorkbook wb = new XSSFWorkbook(file);
            XSSFSheet sheet = wb.getSheet(sheetName);
            int rowCount = sheet.getLastRowNum();
            int column = sheet.getRow(0).getLastCellNum();
            Object[][] data = new Object[rowCount][column];
            for (int i = 1; i <= rowCount; i++) {
                XSSFRow row = sheet.getRow(i);
                for (int j = 0; j < column; j++) {
                    XSSFCell cell = row.getCell(j);
                    DataFormatter formatter = new DataFormatter();
                    String val = formatter.formatCellValue(cell);
                    data[i - 1][j] = val;
                }
            }

            return data;
        }
}

并在测试中像这样使用:

import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;



public class DataProviderDemo {

    private String filePath = "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\demo.xlsx";
    private String sheetName = "demo";

    @Test(dataProvider = "excelData")
    public void read(String username, String password) {
        System.out.println(username + ":" + password);

    }

    @DataProvider(name="excelData")
    public Object[][] readExcel() throws InvalidFormatException, IOException {
        return ExcelRedaer.readExcel(filePath, sheetName);
    }

}

由于您没有ExcelApiTest类的所有方法,因此您的代码中出现错误,可能是当您从获取此代码的位置复制代码时,应该有一个名为ExcelApiTest的类,其中包含诸如{{ 1}}等。

您可以将此代码用于演示目的,用于用户名和密码字段。

您的类名也是DataProvider,它将再次给您一个错误,因为testng也具有此类: getRowCount(SheetName) and getColumnCount(SheetName)。 因此会有冲突,您也应该更改班级名称。

我的excel: enter image description here

希望对您有帮助:)