阅读多个excel表selenium-webdriver,java,eclipse

时间:2017-08-23 12:09:43

标签: java excel eclipse

我想运行 selenium-webdriver-java-eclipse ,使用 excel文件包含具有不同名称的多个 excel表格(sheet1,sheet2) ,sheet3,...),我需要一个for循环帮助我这样做并从这张表中读取。     公共类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;

}

}

2 个答案:

答案 0 :(得分:1)

尝试这样的事情,它对我有用,你需要在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

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

                 }

            }

        }

    }
}

}

答案 1 :(得分:0)

请尝试类似这样的写作:

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

方法中使用的术语非常自我解释。如果您遇到困难或需要更多信息,请告知我们。