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

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

 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(); } 

依赖(poi-ooxml.jar)

 import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

坚持到Excel文件

 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文件中检索,请按照链接, 使用Apache Poi从Excel工作表中获取单元格值 。

HTH