2016-08-17 6 views
2

Mein Javanicht in der Lage, um numerische Werte von Excel-Tabelle in Selen WebDriver mit 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"); 
     } 

} 

Durch die Verwendung von oben Code zu erhalten Ich erhalte eine Fehlermeldung „kann nicht Textwert von numerischer Zelle erhalten“. Irgendeine Hilfe? Auch Meine Ausgabe ist nicht richtig zugeordnet. Alle Saiten zeigen einen unter einem. Ausgabe sollte wie

Username Password 
john  123 
rambo 456  

sein, aber ich bin immer eine Ausgabe wie

Username 
password 
john 

Antwort

2

Ändern Sie Ihre for-Schleife nach // create a loop to print cell values Kommentar hierzu:

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; 

      } 

} 

Switch ist Zellart zu erkennen. Für numerische Zellen haben Sie getNumericCellValue() statt getStringCellValue()

zu verwenden, für das zweite Problem System.out.print() statt System.out.println() verwendet werden, die verwendet wird, zu drucken, was zwischen den doppelten Anführungszeichen ist und den Druck Cursor in der nächsten Zeile zu bewegen.

EDIT:

Dies, wie meine readfile() Funktion sieht:

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

Changed System.out.print(row.getCell(j).getNumericCellValue() + " "); für System.out.print((int)row.getCell(j).getNumericCellValue() + " "); bei Cell.CELL_TYPE_NUMERIC

+0

Benutzername Benutzername || Passwort Passwort || John John || 12345.0 Ausgabe wie folgt (rambo kann nicht drucken) – Ab123

+0

Ich habe meine Readfile-Funktion in meine Antwort eingefügt. Meine Ausgabe sieht so aus: – Gelbi

+0

Code bearbeitet bitte überprüfen – Ab123

Verwandte Themen