2017-08-10 65 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