2017-08-10 63 views
0

見下面我的代碼:數據輸入Excel表,在輸出Excel工作表中自動顯示

FileInputStream fis=new FileInputStream("E://Pincodes Task//Pincodes-List.xlsx"); 
XSSFWorkbook wb=new XSSFWorkbook(fis); 
XSSFSheet sh=wb.getSheetAt(0); 
int totalNoOfRows = sh.getLastRowNum();  
for(int i = 1; i < totalNoOfRows; i++) { 
    XSSFRow row=sh.getRow(i); 
    pincode=new DataFormatter().formatCellValue(row.getCell(0)); 
    fis.close(); 
    driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtPincodeSearch")) 
     .sendKeys(pincode); 
    driver.findElement(By.name("ctl00$ContentPlaceHolder1$btnPincodeSearch")).click();    
    if(driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_lblNoResult']")).isDisplayed()) { 
    String noData="No data for "+pincode+" code"; 
    XSSFSheet sh2=wb.getSheetAt(0);   
    XSSFRow row2=sh2.createRow(i); 
    row2.createCell(0).setCellValue(noData); 
    FileOutputStream fos=new FileOutputStream("E://Pincodes Task//Output-List.xlsx"); 
    wb.write(fos); 
    fos.close(); 
} else { 
    if(driver.findElement(By.id("ctl00_ContentPlaceHolder1_grdPincode_ctl02_HyperLink1")).isEnabled()) { 
     driver.findElement(By.id("ctl00_ContentPlaceHolder1_grdPincode_ctl02_HyperLink1")).click(); 
     driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 
     data=driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_TableCell4']")).getText(); 
     XSSFSheet sh2=wb.getSheetAt(0); 
     XSSFRow row2=sh2.createRow(i); 
     row2.createCell(0).setCellValue(data); 
     FileOutputStream fos=new FileOutputStream("E://Pincodes Task//Output-List.xlsx"); 
     wb.write(fos); 
     fos.close(); 
    } 
} 

我需要從一個Excel工作表中讀取輸入,我需要保存的數據從網站採取的另一個Excel文件,但輸入數據會自動保存在輸出文件中。

+0

你需要讓輸出XLSX單獨XSSFWorkbook比如,你正在使用的輸入和輸出XLSX – vvtx

+0

Thanq @vvtx相同的實例您的回覆,我創建單獨的工作簿例如像這個 XSSFWorkbook wb2 = null;但我得到異常 - 線程「主」java.lang.NullPointerException異常 \t在PostalDetails.main(PostalDetails.java:49) – Krishna

+0

代碼重新格式化,大寫 –

回答

0

查找以下用於讀取和寫入數據的代碼。如果需要,請參閱鏈接 [http://www.seleniumeasy.com/jxl-tutorials/set-data-into-excelsheet-with-jxl]

public static WritableSheet readExcel() 
{ 
    try 
    { 
     //reading the excel file 
     wbook = XSSFWorkbook.getWorkbook(new File("path\\testSampleData.xls")); 
     wwbCopy = XSSFWorkbook.createWorkbook(new File("path\\testSampleDataCopy.xls"), wbook); 
     WritableSheet shSheet = wwbCopy.getSheet(0); 
    } 
    catch (Exception e) 
    { 
     new CustomizedException(e, driver); 
    } 
    return shSheet; 
} 
+0

嗨@Ravi,我得到這個錯誤 - 方法createWorkbook(文件,XSSFWorkbook)是未定義的類型XSSFWorkbook – Krishna

+0

直接你可以在工作簿中提到文件名而不是在fis –

+0

我認爲是它將數據從一個excel文件複製到另一個excel文件? – Krishna