2016-09-21 67 views

回答

0

您需要編寫循環以獲取所有表格值到Excel表格中。下面是我在我的項目中嘗試的代碼。

public void getWebTableValues() throws Exception 
{ 
    WebElement data1=d.findElement(By.xpath("xpath of table")); 
    List<WebElement> tableRows = data1.findElements(By.tagName("tr")); 
    int rowSize=tableRows.size(); 
    System.out.println(rowSize); 
    fis1=new FileInputStream("path of the excel sheet upto ExcelSheet.xlsx"); 
    wb1=new XSSFWorkbook(fis1); 
    st1=wb1.getSheetAt(1); 
    for (int i=0; i<rowSize; i++) 
    { 
     WebElement webRow = tableRows.get(i); 
     //Get all cell values in each row 
     List<WebElement> allCells = webRow.findElements(By.tagName("td")); 
     //System.out.println(allCells.size()); 
     if(allCells.size() > 1) 
     { 
      row = st1.createRow(i); 
      for (int j=0; j<allCells.size(); j++) 
      { 
       WebElement webCell = allCells.get(j); 
       String text = webCell.getText(); 
       if(text.length()>3) 
       { 
        cell = row.createCell(j); 
        cell.setCellValue(webCell.getText()); 
       } 
       FileOutputStream fos=new FileOutputStream("path of the excel sheet upto kpmgExcelSheet.xlsx"); 
       wb1.write(fos); 
      } 
     } 
    } 

希望這有助於你