gpt4 book ai didi

java - 使用 Java 和 JXL/JExcel 读取电子表格

转载 作者:行者123 更新时间:2023-12-01 10:15:31 24 4
gpt4 key购买 nike

我的代码正在打开电子表格并仅读取最后一行...我做错了什么?

Workbook workbook = Workbook.getWorkbook(new File("C:/promocoes.xls"));

//Number of lines of plannilha
Sheet sheet = workbook.getSheet(0);
int linhas = sheet.getRows();

for(int i = 0; i < linhas; i++){

Cell a1 = sheet.getCell(0, i);
Cell a2 = sheet.getCell(1, i);

String as1 = a1.getContents();
String as2 = a2.getContents();

nomepromocao = as1;
tipopromocao = as2;
}
workbook.close();

driver.findElement(By.name("nomePromocao")).sendKeys(nomepromocao);
driver.findElement(By.name("descPromocao")).sendKeys(tipopromocao);

enter image description here

最佳答案

您需要在循环中的下一次迭代之前使用这些值:

Workbook workbook = Workbook.getWorkbook(new File("C:/promocoes.xls"));
Sheet sheet = workbook.getSheet(0);
int rowCount = sheet.getRows();
for(int i = 0; i < rowCount; i++){
String nomePromocao = sheet.getCell(0, i).getContents();
String descPromocao = sheet.getCell(1, i).getContents();

driver.findElement(By.name("nomePromocao"))
.sendKeys(nomePromocao);

driver.findElement(By.name("descPromocao"))
.sendKeys(descPromocao);
}
workbook.close();

您还可以存储这些值以供以后使用:

// to store all the records
List<HashMap> records = new ArrayList<HashMap>();
Workbook workbook = Workbook.getWorkbook(new File("C:/promocoes.xls"));
Sheet sheet = workbook.getSheet(0);
int rowCount = sheet.getRows();
for(int i = 0; i < rowCount; i++){
HashMap<String, String> record = new HashMap<String, String>();
record.put("nomepromocao", sheet.getCell(0, i).getContents());
record.put("tipopromocao", sheet.getCell(1, i).getContents());
records.add(record);
}
workbook.close();

// to iterate each record
for(HashMap<String, String> record : records){

driver.findElement(By.name("nomePromocao"))
.sendKeys(record.get("nomepromocao"));

driver.findElement(By.name("descPromocao"))
.sendKeys(record.get("descPromocao"));

}

关于java - 使用 Java 和 JXL/JExcel 读取电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35925100/

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