2016-02-12 87 views
0

我试图使用Apache POI将数据Excel导出到网页。我已经能够输出数字数据(即123)到excel,但所有的值被导出为浮动(即123.0),我需要他们保持自己的数据类型,即数字Selenium WebDriver Apache POI(无法从excel中读取数值)

  FileInputStream fi = new  FileInputStream("C:\\eclipse\\eclipse\\example\\DRRT.xls"); 

      HSSFWorkbook eb = new HSSFWorkbook(fi); 

      HSSFSheet sh = eb.getSheet("Sheet1"); 

      HSSFRow row=null; 

      HSSFCell cell = null; 

      HSSFCell cell1 =null; 

      for(int i=1;i<=sh.getLastRowNum();i++) 

      { 

      row = sh.getRow(i); 

      cell = row.getCell(0); 

      cell1 = row.getCell(1); 

      System.out.println(i +" login "); 

      System.out.println(cell +" "+cell1); 

      String s = cell.getStringCellValue(); // for user name 

      String s1 = String.valueOf(cell1); // for password 

      WebDriver we = new FirefoxDriver(); 

      we.get("https;//……………"); 

      we.findElement(By.name("txtUsername")).sendKeys(s); 

      we.findElement(By.name("txtPassword")).sendKeys(s1); 

      we.findElement(By.name("btnLogin")).click(); 

      System.out.println(we.getTitle()); 

      Thread.sleep(3000); 

      we.findElement(By.partialLinkText("LOGOUT")).click(); 

      we.close(); 

      } 

我m使用用户名:超级用户 密码:123

但是,在运行此脚本时,数值会转换为浮点数,并且我无法登录。sir,请帮助我阅读单元格值的确切格式从Excel表单中,以便我可以将其传递到网页的文本字段中。

回答

0

将密码字符串加倍然后转换为整数。

String s1 = String.valueOf(cell1); // for password 
s1 =(int)Double.parseDouble(s1) + ""; 

或者如果你把它只有一行:

String s1 = (int) Double.parseDouble(String.valueOf(cell1)) + ""; 

这会给你一个整数输出中的字符串 “123”。

相关问题