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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19405709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:53:17  来源:igfitidea点击:

Writing data into excel using JXL- Selenium(Java)

javaseleniumwebdriverjxl

提问by Chaitanya

I am trying to read from webpage and write it to a excel sheet. Below is set of code works fine, but i am not able to figure out how to run this in a loop so that i can wright bulk data. As i have to write many value which i am reading from the table

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

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

This is the content which i am reading from webpage.

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

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

回答by TheLostMind

First read the entire data you want to write.. Then start from a col say 0 , row number =1 - and then start writing data.. If you are storing the data in an arrayList, then

首先读取要写入的整个数据..然后从列开始说 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
    } 

回答by Chaitanya

Thanks for all your help But the below code worked for me

感谢您的所有帮助但以下代码对我有用

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();