如何提取到Excel以获取行之间没有间隙的列表?

时间:2017-08-01 08:49:30

标签: java apache selenium selenium-webdriver

我从表中提取数据没有问题,但是不喜欢将提取的数据写入其他类型。有关如何避免此问题的任何建议,以便我可以有一个没有间隙的列表?老实说,我不确定如何解决这个问题。也许我在思考它或者需要重新开始。

下面的屏幕截图和代码:

[![截图] [1] [1]

package WebScrapper;

import java.io.PrintWriter;
import java.net.URLEncoder;

import java.util.*; 
import java.lang.*; 
import  java.io.*;

import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver;  

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;




import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;




import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import junit.framework.Assert;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class dads { 

public static void main(String args[]) throws Exception{





    SXSSFWorkbook workbook = new SXSSFWorkbook(100);
    SXSSFSheet sheet = workbook.createSheet("output");
     FileOutputStream f = new FileOutputStream("czPA LEASE.xlsx",true);

  // DRIVER
    WebDriver driver=new FirefoxDriver();
    driver.get("https://en.wikipedia.org/wiki/List_of_Death_Note_episodes");

    List<WebElement> Elements=driver.findElements(By.xpath("//table//tbody//td//i"));



    int x=-1;
    int y=3;
    for(WebElement ele: Elements)
    {
        System.out.println(ele.getText());
        SXSSFRow dataRow = sheet.createRow((short)++x);
        SXSSFCell cell=dataRow.createCell(0);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(ele.getText());
    }




    List<WebElement> Elements1=driver.findElements(By.xpath("//table//tbody//td"));

    int x1=-1;
    int y1=5;
    for(WebElement ele1: Elements1)
    {
        System.out.println(ele1.getText());
        SXSSFRow dataRow = sheet.createRow((short)++x1);
        SXSSFCell cell1=dataRow.createCell(1);
        cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell1.setCellValue(ele1.getText());
    }



    try {
        f.flush();
        workbook.write(f);
        f.close();
        System.out.println("Excel written successfully..");
        driver.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

以下代码对您有所帮助

  public static void main(String args[]) throws Exception{
    SXSSFWorkbook workbook = new SXSSFWorkbook(100);
    SXSSFSheet sheet = workbook.createSheet("output");
     FileOutputStream f = new FileOutputStream("GAHzxvv123TESTvvvzxzzadage12.xlsx",true);

  // DRIVER

    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://en.wikipedia.org/wiki/List_of_Death_Note_episodes");
    List<WebElement> Elements=driver.findElements(By.xpath("//table//tbody//td//i"));
    int x=-1;
    for(WebElement ele: Elements)
    {
        System.out.println(ele.getText());
        SXSSFRow dataRow = sheet.createRow((short)++x);
        SXSSFCell cell=dataRow.createCell(0);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(ele.getText());
    }
    try {
        f.flush();
        workbook.write(f);
        f.close();
        System.out.println("Excel written successfully..");
        driver.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}