2013-08-20 12 views
-3

我試圖編寫從Excel文件中讀取數據的代碼。 excel文件有7列,前5列由字符串型數據組成,最後2列由整型數據組成。現在我想將這些數據發送到一個網頁的形式。要求將整數發送到文本字段的解決方案

問題與我的代碼:我的代碼是返回整個excel數據作爲字符串,因爲我只需要前5列作爲字符串和最後2列作爲整數。此外,雖然網頁形式的所有字段都是文本字段,但我不確定爲什麼字段在發送字符串數據時出現「無效格式」錯誤。你可以看看下面的代碼,並幫助我解決這個問題。

@Test 公共無效SampleCustNumFormat()拋出異常{所有你必須正確地設計你的類,以反映您正在處理,而不是將一切都作爲字符串數據模型的

String vURL; 
    String vEmail; 
    String vPswd; 
    String vCnfmPswd; 
    int vCustAccNum; 
    int vZipCode; 
    long iWait; 
    int Size; 
    // Read Test Data from Excel 

    String xlPath = "C:\\SCE docs\\Automation\\InputData_Registration.xls"; 
    ArrayList<CustomerData> customerData = getExcelData(xlPath,"AccountHolderRegistration"); 
    System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     iWait=3000; 

     for (int k=1; k<xRows; k++) 
     { 


       driver.navigate().to(vURL); 
       driver.findElement(By.linkText("Register")).click(); 
       driver.findElement(By.id("emailInputBox_input")).clear(); 
       driver.findElement(By.id("emailInputBox_input")).sendKeys(vEmail); 
       driver.findElement(By.id("pwdInputBox_input")).clear(); 
       driver.findElement(By.id("pwdInputBox_input")).sendKeys(vPswd); 
       driver.findElement(By.id("confirmpwd_input")).clear(); 
       driver.findElement(By.id("confirmpwd_input")).sendKeys(vCnfmPswd); 
       driver.findElement(By.id("radio1")).click(); 
       driver.findElement(By.id("accountInputBox_input")).sendKeys(Integer.toString(vCustAccNum)); 
       driver.findElement(By.id("accountInputBox_input")).clear(); 
       driver.findElement(By.id("zipcodeBox_input")).sendKeys(Integer.toString(vZipCode)); 
       driver.findElement(By.id("zipcodeBox_input")).clear(); 
       driver.findElement(By.id("terms3")).click(); 
     /*  driver.findElement(By.id("registerButton_label")).click(); 
       driver.findElement(By.id("continueButton_label")).click(); 
       Thread.sleep(iWait); 
      */       

     } 




} 

// Custom Methods 


public ArrayList<CustomerData> getExcelData(String Path, String shtName) 
     throws Exception { 
    ArrayList<CustomerData> customerDataList = new ArrayList<CustomerData>(); 

    File myxl = new File(Path); 
    FileInputStream fi = new FileInputStream(myxl); 
    HSSFWorkbook myWB = new HSSFWorkbook(fi); 
    HSSFSheet mySheet = myWB.getSheet(shtName); 

    xRows = mySheet.getLastRowNum() + 1; 
    xCols = mySheet.getRow(0).getLastCellNum(); 

    for (int i = 1; i < xRows; i++) { 
     HSSFRow row = mySheet.getRow(i); 

     CustomerData customerDetails = new CustomerData(); 
     customerDetails.setvURL(row.getCell(1).getStringCellValue()); 
     customerDetails.setvEmail(row.getCell(2).getStringCellValue()); 
     customerDetails.setvPswd(row.getCell(3).getStringCellValue()); 
     customerDetails.setvCnfmPswd(row.getCell(4).getStringCellValue());   
     customerDetails.setvCustAccNum((int) row.getCell(5).getNumericCellValue()); 
     customerDetails.setvZipCode((int) row.getCell(6).getNumericCellValue()); 
     customerDataList.add(customerDetails); 
     System.out.println("row 2: "+row.getCell(3)); 
    // row.getCell(2).setCellValue(convertFormatCustNo(customerDetails.getCustNo())); 


    } 
    return customerDataList; 

} 
+2

你顯然沒有'寫代碼'。您在抱怨所有數據都以字符串形式返回,並且您有一個顯式名爲'cellToString()'的方法。你認爲這種方法有什麼作用? getExcelData()方法返回一個String [] []'數組,你如何期望它返回其他東西?顯然你沒有寫這些,你希望我們給你的代碼,甚至沒有嘗試自己做任何事情。而輸出甚至不是那個代碼。代碼中沒有任何行輸出'Some Data' – jbx

+0

我不是在抱怨,我知道我接收到的輸出是什麼,我想知道如何將最後兩列從字符串轉換爲整數,然後再輸入網絡頁。我已經發布了另一個問題,他們要求我發佈一個新問題,這就是我發佈此問題的原因。 – user1564024

+0

上面發佈的是我的實際代碼,它返回的字符串數據和轉換爲整數的擴展名我沒有發佈,因爲有一個清楚的理解,誰是假設回答我的問題的人。關於輸出我已經刪除了打印語句。 – user1564024

回答

1

第一。

您可以簡單地創建一個類,反映了一排你的Excel工作表:

public class AccountHolder 
{ 
    private String id; 
    private String url; 
    private String email; 
    private String password; 
    private String confirmPassword; 
    private int accountNumber; 
    private int zipCode; 

    public AccountHolder() 
    { 
    } 

    public String getId() 
    { 
    return id; 
    } 

    public void setId(id) 
    { 
    this.id = id; 
    } 

    //todo: do the rest of the fields in the same way with public getters and setters 

} 

然後換getExcelData()返回一個AccountHolder[]而不僅僅是一堆String對象。

最後修改getExcelData()在循環:

AccountHolder[] accountHolders = new AccountHolder[xRows]; 

for (int i=0;i<xRows;i++) 
{ 
    HSSFRow row = mySheet.getRow(i); 

    AccountHolder accountHolder = new AccountHolder(); 
    accountHolder.setId(row.getCell(0).getStringCellValue()); 

    //todo: the rest of the String fields of AccountHolder 

    accountHolder.setAccountNumber(row.getCell(5).getNumericCellValue()); 
    accountHolder.setZipCode(row.getCell(6).getNumericCellValue()); 

    accountHolders[i] = accountHolder; 
} 

return accountHolders; 

這樣,你有一個AccountHolder實例的每一行,具有其應有的類型每個字段。

+0

我已經使用了arraylist的上述方法,但是代碼要求我初始化代碼中定義的變量,如果我的代碼正在拋出空點異常。你能否查閱更新後的代碼? – user1564024

+0

此外,如果我不將CustomerNum和Zipcode轉換爲整數的字符串,硒的Sendkeys不接受。因此,我無法繼續前進。你能幫我解決這個問題嗎? – user1564024

+0

你從未在你的問題的任何地方提及過硒。只需將所有字段更改爲String並使用您之前發佈的'cellToString()'方法。 – jbx