有人可以提供代码/逻辑来检索excel(.xlsx)数据,如"" driver.findElement(By.id(""))。sendkeys(getExceldata(row,列));"

时间:2016-08-19 16:58:14

标签: java excel selenium apache-poi

有人可以提供代码/逻辑来检索Excel(.xlsx)数据。我需要以这样的方式检索数据,以便我可以获取值并将其传递到用于测试网页的代码中的任何位置。这是我需要获得的代码:

driver.findElement(By.id("")).sendkeys(getExceldata(row, column));

我只需要定义行和列,它应该使用getExceldata(1,2)

之类的任何方法从Excel工作表中获取数据
public String cellValue(String filepath,String sheetname,int r,int c)
{
        try{
            FileInputStream fis = new FileInputStream(new File(filepath));              
            book = WorkbookFactory.create(fis);
            sh = book.getSheet(sheetname);

            System.out.println(sh.getSheetName());              

            row = sh.getRow(r);
            cell = row.getCell(c);

            return cell.getStringCellValue();               
        }catch(Exception e)
        {
            return null;
        }
    }

    public int getRows(String filepath,String sheetname)
    {
     try{
        FileInputStream fis= new FileInputStream(filepath);
        book= new XSSFWorkbook(fis);

        return book.getSheet(sheetname).getLastRowNum();
     }
     catch(Exception e)
     {
         return 0;
     }

----------------------------下面的新课---------------- ----------------

WebDriver driver;

    Excel excel= new Excel();

    public static String filepath="‪D:\\Chinmaya Work\\Work Space\\simpleProject\\src\\Newcustomerdata.xlsx";
    public static String sheetname="newcusotomer";

    public void setUp() throws InterruptedException
    {
        driver=new FirefoxDriver();
        driver.get("http://www.demo.guru99.com/V4/index.php");
        Thread.sleep(5000);
    }

    public void loginToApplication()
    {
        Homepagegurru99 home=new Homepagegurru99(driver);

        home.enterUsername("mngr45812");
        home.enterPassword("chinu@221");
        home.clickLoogin();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    }

    public void gotoNewcustomepage()
    {
        Dashbardgurru99 dash=new Dashbardgurru99(driver);
        dash.gotonewCustomerpage();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    }

    public void createNewcustomer()
    {
        Newcustomeradd create=new Newcustomeradd(driver);           
        int rowCount=excel.getRows(filepath, sheetname);            
        System.out.println(rowCount);


        create.eneterCustomername(excel.cellValue(filepath, sheetname, 2,0));
        create.selectSex();
        create.enterDob(excel.cellValue(filepath, sheetname, 3,0));
        create.enterAddress(excel.cellValue(filepath, sheetname, 4,0));
        create.enterCity(excel.cellValue(filepath, sheetname, 5,0));
        create.enterState(excel.cellValue(filepath, sheetname, 6,0));
        create.enterPin(excel.cellValue(filepath, sheetname, 7,0));
        create.enterMobileno(excel.cellValue(filepath, sheetname, 8,0));
        create.enterEmail(excel.cellValue(filepath, sheetname, 9,0));
        create.enterPassword(excel.cellValue(filepath, sheetname, 10,0));
        create.clickSubmit();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        driver.switchTo().alert().accept();    

    }

    public void logOut()
    {
        driver.findElement(By.linkText("Log out")).click();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        driver.switchTo().alert().accept();

        driver.close();
    }

1 个答案:

答案 0 :(得分:0)

依赖关系(poi-ooxml.jar)

fis = new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);

XSSFRow row1 = sheet.createRow(0);
XSSFCell r1c1 = row1.createCell(0);
r1c1.setCellValue("Demo");

fis.close();

FileOutputStream fos = new FileOutputStream(new File(outputFile));
workbook.write(fos);
fos.close();

要保留到Excel文件

strace -p <PID>

要从Excel文件中检索,请点击Get Cell Value from Excel Sheet with Apache Poi链接。

HTH