简体   繁体   English

读取多个Excel工作表selenium-webdriver,java,eclipse

[英]read multiple excel sheet selenium-webdriver, java, eclipse

I want to run selenium-webdriver-java-eclipse , using excel file contains multiple excel sheets with different name(sheet1,sheet2,sheet3,...), i need a for loop help me to do that and read from this sheets. 我想运行selenium-webdriver-java-eclipse ,使用excel文件包含多个具有不同名称(sheet1,sheet2,sheet3,...)的Excel工作表,我需要一个for循环来帮助我做到这一点并从此工作表中读取。 public class ExcelDataConfig { 公共类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;

}

} }

try something like this, it is working for me you need to add the sheet numbers and cell numbers at the places of k and j 尝试这样的事情,它对我有用,您需要在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);

//condetions // condetions

                /* 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){

                 }

            }

        }

    }
}

} }

Please try writing similar to something like this: 请尝试编写类似于以下内容的内容:

for (int i = startRow; i < endRow + 1; i++) {
                for (int j = startCol; j < endCol + 1; j++) {
                    testData[i - startRow][j - startCol] = ExcelWSheet.getRow(i).getCell(j).getStringCellValue();
                    Cell cell = ExcelWSheet.getRow(i).getCell(j);
                    testData[i - startRow][j - startCol] = formatter.formatCellValue(cell);
                }
            }

Terms used in method are pretty self explanatory. 方法中使用的术语很容易解释。 Let us know if you get stuck or need more info. 让我们知道您是否遇到困难或需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Selenium-Webdriver中将Java变量与Sikuli一起使用 - Using Java variables with Sikuli in Selenium-Webdriver 数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写 - Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java 如何从Selenium Webdriver中的Excel工作表中读取数据 - How to read Data from Excel sheet in selenium webdriver 如何使用 java 通过 selenium-webdriver 和 Java 鼠标 hover - How to mouse hover using java through selenium-webdriver and Java 如何通过Selenium-Webdriver和Java处理分页 - How to handle pagination via selenium-webdriver and Java 如何使用 selenium-webdriver 和 Java 单击日历图像图标 - How to click the Calendar image icon using selenium-webdriver and Java 使用 Selenium-WebDriver 通过 AutoIt 上传多个文件 - Multiple file upload through AutoIt using Selenium-WebDriver 如何使用selenium-webdriver和Java查找元素 - How to locate the element using selenium-webdriver and Java 无法使用Java切换到Selenium-WebDriver中的对话框? - Unable to switch to a dialog box in Selenium-WebDriver using Java? Selenium-Webdriver NodeJS 相当于 DesiredCapabilities 的 Java 代码 - Selenium-Webdriver NodeJS Equivalent to Java Code for DesiredCapabilities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM