如何将列表提取为Excel?

时间:2017-08-02 09:25:58

标签: apache selenium selenium-webdriver

如果这是一个愚蠢的问题,我道歉。但为什么我只能将一个列表提取到excel?我已经尝试修改它,但我真的不明白为什么我不能将2列与提取的数据并排。而是显示一个。

任何修复?

我有以下代码:

`<style>
@font-face {
    font-family: OptimusPrinceps;
    src: url('{{asset('/fonts/OptimusPrinceps.tff')}}');
}
</style>`

1 个答案:

答案 0 :(得分:0)

我希望这能帮助您找到答案:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

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;
public class Test {

    static WebDriver driver = new FirefoxDriver();
    static WebElement element ;
    public static void main(String[] args) throws InterruptedException, IOException {
        String excelFileName = "C:/Test.xls";//name of excel file

        String sheetName = "Sheet1";//name of sheet

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetName) ;
        // DRIVER
        WebDriver driver=new FirefoxDriver();
        driver.get("https://en.wikipedia.org/wiki/List_of_Death_Note_episodes");

        List<WebElement> rows=driver.findElements(By.xpath(".//table[@class='wikitable'][1]//th[contains(@id,'ep')]"));
        //iterating r number of rows
        for (int r=0;r < rows.size(); r++ )
        {
            HSSFRow row = sheet.createRow(r);
            List<WebElement> cloumns = driver.findElements(By.xpath(".//table[@class='wikitable'][1]//th[@id='ep"+r+"']//parent::tr[1]/td"));
            //iterating c number of columns
            for (int c=0;c < cloumns.size(); c++ )
            {
                HSSFCell cell = row.createCell(c);

                cell.setCellValue(cloumns.get(c).getText());
            }
        }

        FileOutputStream fileOut = new FileOutputStream(excelFileName);

        //write this workbook to an Outputstream.
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
    }
}