gpt4 book ai didi

java - 如何在POM maven项目中使用数据提供程序从excel表中读取数据

转载 作者:行者123 更新时间:2023-12-02 02:01:21 26 4
gpt4 key购买 nike

我无法使用数据提供程序从 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;
}
}

最佳答案

您可以使用以下代码作为用户名和密码,并修改它以获取更多列或字段:

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 的类,其中包含一些方法,例如 getRowCount(SheetName) 和 getColumnCount(SheetName)

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

此外,您的类名称是 DataProvider,这将再次给您带来错误,因为 testng 也有此类:org.testng.annotations.DataProvider。所以会出现冲突,你也应该更改你的类名。

我的Excel: enter image description here

希望对你有帮助:)

关于java - 如何在POM maven项目中使用数据提供程序从excel表中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com