2016-08-17 58 views
2

我的Java无法从Excel工作表中硒的webdriver数值与Java

public class Readexcel { 
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(row.getCell(j).getNumericCellValue() + " "); 
        break; 
      } 
      System.out.print(row.getCell(j).getStringCellValue()+"||"); 

     } 


    System.out.println(); 
    } 


    } 

    public static void main(String...strings) throws IOException 
    { 
     Readexcel re = new Readexcel(); 
     String filepath = "F://Excelsheet"; 
     re.readfile(filepath,"Book1.xlsx","Sheet1"); 
     } 

} 

通过使用上面的代码中,我得到一个错误“无法从数字单元格文本值”。任何帮助?此外我的输出未正确对齐。所有的字符串都显示一个一个。输出应该像

Username Password 
john  123 
rambo 456  

,但我完成后,该// create a loop to print cell values评论越来越像输出

Username 
password 
john 

回答

2

更改您的循环:

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; 

      } 

} 

交换机是识别细胞的类型。对于数字的细胞,你必须使用getNumericCellValue()代替getStringCellValue()

对于第二个问题,请使用System.out.print()代替System.out.println()这是用来打印什么是双引号之间和移动打印光标移动到下一行。

编辑:

这是我READFILE()函数的外观:

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

} 

EDIT 2

的情况下,变更为System.out.print(row.getCell(j).getNumericCellValue() + " "); System.out.print((int)row.getCell(j).getNumericCellValue() + " ");Cell.CELL_TYPE_NUMERIC

+0

用户名用户名||密码密码||约翰约翰|| 12345.0得到这样的输出(rambo无法打印) – Ab123

+0

我在我的答案中粘贴了我的readfile函数。我的输出如下所示: – Gelbi

+0

已编辑的代码请检查 – Ab123