简体   繁体   English

从webtable中获取值并在循环中的每一行中一一写入excel表中

[英]Fetching value from webtable and write in excel sheet one by one of each row in a loop

1) I am trying to read the values from the webtable of an appliation and write it into the excel sheet one by one. 1)我试图从应用程序的网络表中读取值并将其一一写入excel表中。

2) There are 4 values in each row of the webtable that need to be written to excel sheet, but there are some images in each row that i'm ignoring bu using below code. 2) webtable 的每一行有 4 个值需要写入 Excel 工作表,但每行中有一些图像我使用下面的代码忽略了 bu。

 text.length()>2
. .

3) There will be 200-300 rows in webtable that need to be fetched and written to excel sheet. 3)webtable 中会有 200-300 行需要提取并写入 Excel 表格。

This is the code i have tried.这是我试过的代码。 But i dont know how to write it into each row of the excel sheet one by one.但我不知道如何将其一一写入excel表的每一行。 Please help me in this regard.请在这方面帮助我。

 //get the table WebElement statusTable = browser.findElement(By.id("projectstatus")); //Get all the rows in the table List<WebElement> allRows = statusTable.findElements(By.tagName("tr")); //Get the size(row no) of allRows int rowSize = allRows.size(); System.out.println(rowSize); for (WebElement row : allRows) { //Get all cell values in each row List<WebElement> allCells = row.findElements(By.tagName("td")); //System.out.println(allCells.size()); if(allCells.size() > 1){ for (WebElement cell : allCells) { String text = cell.getText(); if(text.length()>2){ String value = cell.getText(); } } } // locate the test xl file File file = new File("e:\\\\Testing_emi.xls"); // create input stream FileInputStream fis = new FileInputStream(file); // create workbook HSSFWorkbook wb = new HSSFWorkbook(fis); // get sheet HSSFSheet sheet1 = wb.getSheet("Sheet1"); // get rows HSSFRow row = sheet1.getRow(1); HSSFCell cellEx = row.getCell(0); if (cellEx == null) { cellEx = row.createCell(0); } cellEx.setCellValue(value);

//get the table
    WebElement statusTable = browser.findElement(By.id("projectstatus"));

    //Get all the rows in the table
    List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));

    //Get the size(row no) of allRows
    int rowSize = allRows.size();
    System.out.println(rowSize);


    // locate the test xls file             
  File file = new File("e:\\Testing_emi.xls");              
  // create input stream                
  FileInputStream fis = new FileInputStream(file);              
  // create workbook                
  HSSFWorkbook wb = new HSSFWorkbook(fis);              
  // get sheet              
  HSSFSheet sheet1 = wb.getSheet("Sheet1");             
  // get rows               


   HSSFRow row;             

    for (int i=0; i<rowSize; i++) 
    {
        WebElement webRow = allRows.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)
        {
            HSSFRow excelRow = sheet1.createRow(i);

            for (int j=0; j<allCells.size(); j++) 
            {
                WebElement webCell = allCells.get(j);
                String text = webCell.getText();
                if(text.length()>2)
                {
                    Cell excelCell = excelRow.createCell();
                    excelCell.setValue(webCell.getText());
                }                   
            }
        }
    }

  sheet1.close();

Read the values from the Web Table of an application and write it in to the excel sheet one by one.There are 5 values in each row of the Web Table and that need to written in Excel sheet.从应用程序的Web Table 中读取值并逐个写入Excel 表格中。Web Table 每行有5 个值需要写入Excel 表格。 Below is the working code.下面是工作代码。

System.setProperty("webdriver.chrome.driver","C:\\Users\\Documents\\Selenium jars\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://cosmocode.io/automation-practice-webtable/");
        WebElement table = driver.findElement(By.xpath("//*[@id='countries']/tbody"));
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        int rowcount = rows.size();
        FileOutputStream fis = new FileOutputStream(new File("C:\\Users\\Documents\\Selenium\\output.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sh = wb.createSheet("First Sheet");
        for (int row = 0; row < rowcount; row++) {
            List<WebElement> columns_inrow = rows.get(row).findElements(By.tagName("td"));
            int columns_count = columns_inrow.size();
            System.out.println("Number of cells in Row " + row + " are " + columns_count);
            Row ro = sh.createRow(row);
            for (int column = 0; column < columns_count; column++) {

                String celltext = columns_inrow.get(column).getText();
                System.out.println(
                        "Cell Value of row number " + row + " and column number " + column + " Is " + celltext);
                ro.createCell(column).setCellValue(celltext);
            }
            System.out.println("===========================");

        }
        wb.write(fis);
        wb.close();

    }

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

相关问题 在Excel中编写Webtable值 - Write Webtable Values in Excel 如何获得Excel工作表的每一行和每一列的值 - How to get the excel sheet each row and column value 如何在Excel工作表的一个哈希图中存储第一行(标题),并在另一个哈希图中存储列数据 - How to store first row (headers) in one hash map from Excel sheet and store column data in another hashmap JPA从多对一关系中获取价值 - JPA fetching value from Many to One relationship 无法使用POI在excell工作表中写多行 - Cant write more than one row in excell sheet using POI 如何使用POI API获取一个Excel工作表的完整行并检查该行是否存在于另一个工作表中? - How to get complete row of one excel sheet using POI API and check the row whether it is present in another sheet? 当我尝试使用循环在 excel 表中写入行和列时,仅打印最后一个循环值 - When i Try to write row and column in an excel sheet using a loop only the last loop values gets print 使用POI从Excel工作表的下一行获取价值 - Get value from next row in Excel sheet with POI 在for循环中使用row.getlastcellnum()方法在Selenium中写入Excel工作表会导致java.lang.IllegalArgumentException - Write excel sheet in Selenium using row.getlastcellnum() method in for loop causes java.lang.IllegalArgumentException 使用Java读取Excel工作表的每一行 - Reading each row of excel sheet using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM