2013-10-16 39 views
0

我想從網頁讀取數據並將其寫入Excel表格。 下面是一組代碼工作正常,但我無法弄清楚如何在循環中運行此操作,以便我可以批量批量數據。正如我寫這我從表使用JXL-Selenium(Java)將數據寫入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); 
+0

你需要你的價值觀從某處刮網頁存儲。考慮使用「集合」類。 – Meesh

回答

0

首先讀取要寫入的全部數據。然後從山坳開始說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 
    } 
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();