Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

selenium-webdriver - 如何在写入Excel时阻止POI附加数据(How to stop POI from appending the datat while writing onto Excel)

There are some data in webpage in a tabular format.

(网页中有一些表格格式的数据。)

I want to retrieve all and write into an excel.

(我想检索所有内容并将其写入Excel。)

I have used a nested for loop.

(我使用了嵌套的for循环。)

But the code is only writing only the last column.

(但是代码只写了最后一列。)

While trying to retrieve and write onto the console, I could see that all the elements are being written, That means, although the code is taking all elements, i guess it is somehow appending the data and only keeping the latest column.

(在尝试检索并写入控制台时,我可以看到所有元素都已写入,这意味着,尽管代码占用了所有元素,但我想它是在某种程度上附加了数据并仅保留最新的列。)

Code used is below.

(使用的代码如下。)

Please help if possible.

(如果可以的话请帮忙。)

    public class FirstTest {
    WebDriver driver;
    XSSFSheet sheet;
    int Rownum;
    FileInputStream  Fis;
    XSSFWorkbook WB;
    FileOutputStream fos;
  @Test
  public void Test1() throws IOException {
      Rownum = driver.findElements(By.cssSelector("tr.one")).size();
      fos = new FileOutputStream("path to excel");

      //System.out.println(Rownum);
      for (int i =0; i<Rownum;i++)
      {       
          for (int j=0;j<=3;j++)
          {
              //System.out.println(driver.findElement(By.xpath("//tr//tr["+(j+1)+"]//td["+(i+1)+"]//div")).getText());
              sheet.createRow(i+1).createCell(j).setCellValue(driver.findElement(By.xpath("//tr//tr["+(i+2)+"]//td["+(j+1)+"]//div")).getText());   
          }         

      }   
      WB.write(fos);
      fos.close();  
        }
  @BeforeSuite
  public void beforeSuite() throws IOException {
      WebDriverManager.firefoxdriver().setup();
      driver = new FirefoxDriver();
      driver.get("https://www.annauniv.edu/dean.php");
        Fis = new FileInputStream("path to excel");
       WB = new XSSFWorkbook(Fis);
       sheet = WB.getSheetAt(0);

  }


  @AfterSuite
  public void afterSuite() throws IOException {

     driver.quit();

  }

}
  ask by Pratilipi Behera translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sheet.createRow does exactly what it says.

(Sheet.createRow完全按照其说的做。)

It creates a new empty row every time it gets called.

(每次调用它都会创建一个新的 行。)

So all cells which previous were created in that row get lost.

(因此,先前在该行中创建的所有单元都将丢失。)

Do:

(做:)

...
XSSFSheet sheet;
XSSFRow row; 
...
for (int i = 0; i < Rownum; i++) {
    row = sheet.createRow(i+1);
    for (int j = 0; j <= 3; j++) {
        //System.out.println(driver.findElement(By.xpath("//tr//tr["+(j+1)+"]//td["+(i+1)+"]//div")).getText());
        row.createCell(j).setCellValue(driver.findElement(By.xpath("//tr//tr["+(i+2)+"]//td["+(j+1)+"]//div")).getText());
    }
}   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...