输入Excel工作表中的数据将自动显示在输出Excel工作表中

时间:2017-08-10 09:55:54

标签: selenium-webdriver apache-poi

请参阅下面的代码:

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文件中保存从网站获取的数据,但输入数据会自动保存在输出文件中。

1 个答案:

答案 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;
}