gpt4 book ai didi

java - 读取多个 excel 表 selenium-webdriver、java、eclipse

转载 作者:行者123 更新时间:2023-12-02 12:20:17 24 4
gpt4 key购买 nike

我想运行selenium-webdriver-java-eclipse,使用excel文件包含多个不同名称的excel工作表(sheet1,sheet2 ,sheet3,...),我需要一个 for 循环帮助我做到这一点并从这张表中读取。 公共(public)类 ExcelDataConfig {

XSSFWorkbook wb;
XSSFSheet sheet = null;

public ExcelDataConfig(String Excelpath) throws IOException {
// TODO Auto-generated method stub

try {
File file = new File(Excelpath);

// Create an object of FileInputStream class to read excel file

FileInputStream fis = new FileInputStream(file);
wb = new XSSFWorkbook(fis);

} catch (Exception e) {

}
}

public String GetData(int sheetNumber, int Row, int Column) {

Iterator<Row> rowIt=sheet.rowIterator();

DataFormatter formatter = new DataFormatter();
XSSFCell cell = sheet.getRow(Row).getCell(Column);

String data = formatter.formatCellValue(cell);
return data;
}

public int GetRowCount(String sheetNumber) {

int row = wb.getSheet(sheetNumber).getLastRowNum();

row = row + 1;

return row;

}

}

最佳答案

尝试这样的事情,它对我有用,你需要在 k 和 j 的位置添加工作表编号和单元格编号

enter code here

String filePath="C:\\Users\\USER\\Desktop\\Book1.xlsx";// file path
FileInputStream fis=new FileInputStream(filePath);
Workbook wb=WorkbookFactory.create(fis);
ArrayList<String> ls=new ArrayList<String>();
for(int k=0; k<=3;k++)//k =sheet no
{
Sheet sh=wb.getSheetAt(k);
System.out.println(sh);
// int count=0;
for(int i=0;i<=sh.getLastRowNum();i++)
{
System.out.println("row no:"+i);
for(int j=0; j<=4;j++)//j=column no
{
try {
String values=sh.getRow(i).getCell(j).getStringCellValue().trim();
System.out.println(values);

//条件

                /* if(values.contains("condtn1"))
{
System.out.println("Value of cell "+values+" ith row "+(i+1));
ls.add(values);
count++;
}
if(values.contains("condn2"))
{
System.out.println("Value of cell "+values+" ith row "+(i+1));
ls.add(values);
count++;
}*/
}catch(Exception e){

}

}

}

}
}

}

关于java - 读取多个 excel 表 selenium-webdriver、java、eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839336/

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