2017-05-28 47 views
-1

我对Selenium非常陌生,并且通过自学习来学习。任何帮助将是可观的。如何从Excel文件单元格中获取数值,使用POI

目前我使用

String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue();

它工作正常为弦乐而不是数字。我的单元格值为'500',我想取它。请帮忙。

+0

https://stackoverflow.com/questions/27688250/how-to-read-numeric-values-from-excel-in-java –

+0

@Debanjan有如下定义的方法:公共字符串的getData( int sheetnum,int row,int col) \t {\t \t String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue(); \t返回数据;我想选择的数字字段是driver.findElement(By.xpath(“// input [@ id ='Weight']”))。sendKeys(excel.getData(1,1,9)); –

+0

请让我知道你需要更多的细节。 –

回答

0

试试下面的代码:

public void readfile(String filepath, String filename, String sheetname) throws IOException { 


    File file = new File(filepath+"\\"+filename); 
    FileInputStream fis = new FileInputStream(file); 

    // for creating .xlsx workbook 
    Workbook wb = new XSSFWorkbook(fis); 

    // for reading the sheet by its name 
    Sheet sh = wb.getSheet(sheetname); 

    // find the total rows in sheet 

    int rowcount = sh.getLastRowNum() - sh.getFirstRowNum(); 

    // create a loop to create 

    for (int i = 0; i < rowcount + 1; i++) { 
     Row row = sh.getRow(i); 

     // create a loop to print cell values 

     for (int j = 0; j < row.getLastCellNum(); j++) { 
      Cell cell = row.getCell(j); 
      switch (cell.getCellType()) { 
      case Cell.CELL_TYPE_STRING: 
       System.out.print(row.getCell(j).getStringCellValue() + " "); 
       break; 

      case Cell.CELL_TYPE_NUMERIC: 
       System.out.print((int)row.getCell(j).getNumericCellValue() + " "); 
       break; 

      } 

     } 

     System.out.println(); 
    } 

} 

希望它会帮助你。

+0

谢谢@Debanjan和@RNS!不知何故,我设法用下面的代码来做到这一点。 –

+0

谢谢@RNS。不知何故,我设法使用下面的代码,它的工作。 public String getNumericData(int sheetnum,int row,int col) \t { \t String data = String.valueOf((int)wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue()); \t返回数据; \t} –

0
Double data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue(); 
相关问题