如何将程序结果写入Excel?

时间:2016-05-28 22:22:23

标签: java excel webdriver apache-poi xls

我正在尝试从网站捕获所有链接,将其打印出来,然后将它们存储在Excel文件中。我的程序正确显示结果,但没有将它们存储在Excel文件中。

package Frameworks;

public class ArrayConversion {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://only-testing-blog.blogspot.in/2013/09/test.html");
        List<WebElement> li = driver.findElements(By.tagName("a"));
        Iterator<WebElement> itr = li.iterator();
        while (itr.hasNext()) {
            String link = itr.next().getText();
            ArrayList<String> stringList = new ArrayList<String>();
            stringList.add(link);

            String array[] = stringList.toArray(new String[stringList.size()]);
            for (String str : array) {
                System.out.println(str);
                xlWrite(array);
            }
        }
    }
    public static void xlWrite(String[] array) throws Exception {
        System.out.println("Inside XL Write");
        File fil = new File("C:\\Users\\Kshitij\\Desktop\\linkstest.xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sht = wb.createSheet("Results");
        for (int i = 0; i < array.length; i++) {
            HSSFRow row = sht.createRow(i);
            for (int j = 0; j < 1; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(array[i]);
            }
            FileOutputStream fout = new FileOutputStream(fil);
            wb.write(fout);
            fout.flush();
            fout.close();
            wb.close();
        }
    }
}

0 个答案:

没有答案