使用JXL-Selenium(Java)将数据写入excel

时间:2013-10-16 14:09:32

标签: java selenium webdriver jxl

我正在尝试从网页上读取并将其写入Excel工作表。 下面是一组代码工作正常,但我无法弄清楚如何在循环中运行它,以便我可以怀好批量数据。因为我必须写出许多我从表中读到的价值

FileOutputStream fo = new FileOutputStream("D:\\output.xls");
WritableWorkbook wb = Workbook.createWorkbook(fo);
WritableSheet ws = wb.createSheet("customsheet", 1);

这是我从网页上阅读的内容。

String m1 = (driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolderBody_ucModelDataEntry1_lblPublishedFuelCostPerLoadEstimatedAllInCost']")).getText());
ws.getCell(m1);

2 个答案:

答案 0 :(得分:1)

感谢您的帮助 但是下面的代码对我有用

String m1 = (driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolderBody_ucModelDataEntry1_lblPublishedFuelCostPerLoadEstimatedAlInCost']")).getText());
System.out.println(m1);
WritableWorkbook wb = Workbook.createWorkbook(new File("D:\\output_2.xls"));
writableSheet ws = wb.createSheet("customsheet",1);
{
Label label = new Label(0,0,m1);
ws.addCell(label);
}
wb.write();
wb.close();

答案 1 :(得分:0)

首先读取你要写的整个数据..然后从col开始说0,行号= 1 - 然后开始写数据..如果你将数据存储在arrayList中,那么

rowNo=1;
for(int colNo=0;colNo<arrList.size();colNo++)
   {
    Cell c = sheet0.getCell(colNo, rowNo);
  //    write data to your cell here... one by one - reading data from arraylist
    }