2016-06-10 43 views
-2

我有一個類似於不同參數的方案,我的方法中的變量應該從Excel工作表中取數據並取其值。例如,有一個像countryname這樣的變量,在Excel工作表中有一個帶有國家名稱列表的第8列。所以當我第一次運行代碼時,countryname變量應該在第8列中取第1個單元格的值,下一次我運行代碼時,它應該取第8列中的第2個單元格的值。同樣,還有其他變量應該從Excel工作表中獲取數據。我如何在java中自動執行這個場景?Selenium Webdriver從Excel工作表中讀取數據

+0

使用Apache POI或JExcel讀取excel文件 –

回答

0

您可以在項目中添加Apache POI jar文件以使用Excel(XLSX)文件。 此外,附上示例代碼供您參考

void readXLSXFile(String fileName) throws IOException 
    { 
     FileInputStream fs= new FileInputStream(fileName); 
     XSSFWorkbook wb = new XSSFWorkbook(fs); 

     XSSFSheet ws = wb.getSheetAt(0);//you can change the value to index value of your required sheet 
     Iterator<Row> rows = ws.rowIterator(); 

     XSSFRow row;    
     while(rows.hasNext()) 
     { 
     row=(XSSFRow) rows.next(); 
     XSSFCell cell=row.getCell(7);//8th cell of the row 
     String CName= cell.getStringCellValue(); //getting the cell value as string 

     countryNames(CName);// Your method for passing the country name to the method for your requirement 
     } 

     wb.close(); 
    } 
相關問題