2015-04-25 61 views
1

我想在excel文件中使用hashmap值來寫入testcase狀態,但它並沒有將數據寫入excel。這裏是我的代碼 -Selenium Java - 將hashmap值寫入excel

public void readData() throws Exception { 
    String filePath = System.getProperty("user.dir") + "\\Test Data"; 
    String fileName = "editSubscriptions.xls"; 
    String sheetName = "datapool"; 

    File file = new File(filePath + "\\" + fileName); 
    FileInputStream fis = new FileInputStream(file); 
    Workbook workbook = null; 

    String fileExtName = fileName.substring(fileName.indexOf(".")); 

    if (fileExtName.equals(".xlsx")) { 
     workbook = new XSSFWorkbook(fis); 
    } else if (fileExtName.equals(".xls")) { 
     workbook = new HSSFWorkbook(fis); 
    } 
    Sheet sheet = workbook.getSheet(sheetName); 
    int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); 
    HashMap<String, String> hm = new HashMap<String, String>(); 
    for (int i = 1; i < rowCount + 1; i++) { 
     Row row = sheet.getRow(i); 
     for (int j = 1; j < row.getLastCellNum(); j++) { 
      System.out.println(row.getCell(j).getStringCellValue()); 
      driver.findElement(By.linkText("Login")).click(); 
      WebDriverWait wait = new WebDriverWait(driver, 10); 
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); 
      driver.findElement(By.id("username")).sendKeys(row.getCell(0).toString()); 
      driver.findElement(By.id("password")).sendKeys(row.getCell(1).toString()); 
      driver.findElement(By.name("submit")).click(); 
      Thread.sleep(10000); 
      try { 
       driver.findElement(By.linkText("Logout")).click(); 
       WebDriverWait wait1 = new WebDriverWait(driver, 10); 
       wait1.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Login"))); 
       hm.put(row.getCell(0).toString(), "Pass"); 
      } catch (Exception ex) { 
       hm.put(row.getCell(0).toString(), "Fail"); 
       driver.get("http://www.openclinica.com"); 
      } 
     } 
    } 
    Set<String> keys = hm.keySet(); 
    for (String key: keys){ 
     System.out.println("Value of "+key+" is: "+hm.get(key)); 

     String filePath1 = System.getProperty("user.dir") + "\\Test Data"; 
     String fileName1 = "editSubscriptions1.xls"; 
     String sheetName1 = "datapool"; 

     File file1 = new File(filePath1 + "\\" + fileName1); 
     FileInputStream fis1 = new FileInputStream(file1); 
     Workbook workbook1 = null; 

     String fileExtName1 = fileName1.substring(fileName1.indexOf(".")); 

     if (fileExtName1.equals(".xlsx")) { 
      workbook1 = new XSSFWorkbook(fis1); 
     } else if (fileExtName1.equals(".xls")) { 
      workbook1 = new HSSFWorkbook(fis1); 
     } 
     Sheet sheet1 = workbook1.getSheet(sheetName1); 
     int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); 

     for (int i=1; i < rowCount1; i++){ 
      Cell cell = sheet1.getRow(i).createCell(2); 
      cell.setCellType(Cell.CELL_TYPE_STRING); 
      cell.setCellValue(hm.put(key, hm.get(key))); 
     } 
    } 
} 

我想通過使用HashMap的鍵添加狀態及格或不及格的Excel工作表。我可以在控制檯中打印狀態,但這不會轉換爲Excel表格。

HemaSai的價值是:失敗 值MoulikaNimmala的是:通過 值凱沙夫的是:失敗

請幫我解決這個問題..

預先感謝您。

+0

什麼是在excelFile結果? –

+0

用戶名密碼\t結果 MoulikaNimmala \t $ Ai11081988 HemaSai \t sai11081988 凱沙夫\t sai30041990 –

+0

這是一排?我無法想象此刻哪些是行和哪些單元格 –

回答

0
int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); 

    for (int i=1; i < rowCount1; i++){ 
     Cell cell = sheet1.getRow(i).createCell(2); 
     cell.setCellType(Cell.CELL_TYPE_STRING); 
     cell.setCellValue(hm.put(key, hm.get(key))); 
    } 

根據doc,您的循環應該從索引0開始。

它看起來像你永遠不會進入一個循環,因爲你從索引1開始,但你的行數最初是0,如果你從一個空白表開始。

編輯:意見後,我會去解決這樣的(未經測試!)

Set<String> keys = hm.keySet(); 
String filePath1 = System.getProperty("user.dir") + "\\Test Data"; 
    String fileName1 = "editSubscriptions1.xls"; 
    String sheetName1 = "datapool"; 

    File file1 = new File(filePath1 + "\\" + fileName1); 
    FileInputStream fis1 = new FileInputStream(file1); 
    Workbook workbook1 = null; 

    String fileExtName1 = fileName1.substring(fileName1.indexOf(".")); 

    if (fileExtName1.equals(".xlsx")) { 
     workbook1 = new XSSFWorkbook(fis1); 
    } else if (fileExtName1.equals(".xls")) { 
     workbook1 = new HSSFWorkbook(fis1); 
    } 
    Sheet sheet1 = workbook1.getSheet(sheetName1); 
    int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); //set rowNumber to the last already set one 
for (String key: keys){ //for every key write the name in column 1 and the result in column 2 
    rowcount1++; //start at the next free row 

    Cell cell1 = sheet1.createRow(rowCount1).createCell(1); 
     Cell cell2 = sheet1.getRow(rowCount1).createCell(2); 
     cell1.setCellType(Cell.CELL_TYPE_STRING); 
     cell2.setCellType(Cell.CELL_TYPE_STRING); 
     cell1.setCellValue(key)); 
     cell2.setCellValue(hm.get(key))); 
    } 
} 
+0

我有意使用的索引開始在1,因爲我的Excel文件中的第0列被命名爲結果/狀態。所以我想狀態通過或失敗從第-1行,第2列 –

+0

開始,但據我瞭解,您正在迭代上面提到的這個循環中的行 –

+0

此代碼持有狀態的值或者Pass或失敗 - 設置 keys = hm.keySet(); (鍵值:鍵){ System.out.println(「+ key +」的值爲:「+ hm.get(key)); \t \t 最後我用excel工作表遍歷了no。 excel中的行從第2列的索引1開始。 因此,我期望在Set中找到的每個關鍵值都應該傳遞給Excel Sheet1中的單元格值。如果這種方法不正確? 請重新推薦我一個更好的方法。 –