2016-08-17 64 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