gpt4 book ai didi

java - 从 Excel 工作表中读取数据

转载 作者:行者123 更新时间:2023-11-30 05:33:39 24 4
gpt4 key购买 nike

从Excel工作表读取数据(java)

我已经创建了使用java实现自动化从excel表读取数据到我的登录网页(用户名和密码)。所以我想知道为什么这段代码在eclipse中运行得不好?

我使用了 TestNg 框架并尝试使用 dataprovider 。它显示此错误“java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap”

1.Dataprovider测试类

private String filePath = "C:\\Users\\samudil\\eclipse-workspace\\ZharaProject\\src\\Excelsheet\\TestData.xlsx";
private String sheetName = "Sheet1";

@Test(dataProvider = "excelData")
public void read(String username, String password) throws InterruptedException {

//handle popup window
Set<String> windowId = driver.getWindowHandles(); // get window id of current window
Iterator<String> itererator = windowId.iterator();

String mainWinID = itererator.next();
String newAdwinID = itererator.next();

driver.switchTo().window(newAdwinID);
System.out.println(driver.getTitle());
Thread.sleep(3000);

driver.findElement(By.xpath("//input[@id='j_username']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"j_password\"]")).sendKeys(password);
driver.findElement(By.xpath(".//*[@id='account']/a")).click();


}

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

2.TestUtil类

public class TestUtil {

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;
}

}

最佳答案

假设您已经添加了所有必需的依赖项(根据您的评论)。请尝试以下步骤,其中之一可能会有所帮助。

  1. 重新启动eclipse
  2. 清理项目(右键单击项目 -> 运行方式 -> Maven Clean)
  3. 更新项目(右键单击项目 -> Maven -> 更新项目 -> 选择您的项目 -> 确定)

如果执行这些步骤后您仍然遇到此问题,则可能遇到了其他问题。

关于java - 从 Excel 工作表中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036443/

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