我試圖編寫從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;
}
你顯然沒有'寫代碼'。您在抱怨所有數據都以字符串形式返回,並且您有一個顯式名爲'cellToString()'的方法。你認爲這種方法有什麼作用? getExcelData()方法返回一個String [] []'數組,你如何期望它返回其他東西?顯然你沒有寫這些,你希望我們給你的代碼,甚至沒有嘗試自己做任何事情。而輸出甚至不是那個代碼。代碼中沒有任何行輸出'Some Data' – jbx
我不是在抱怨,我知道我接收到的輸出是什麼,我想知道如何將最後兩列從字符串轉換爲整數,然後再輸入網絡頁。我已經發布了另一個問題,他們要求我發佈一個新問題,這就是我發佈此問題的原因。 – user1564024
上面發佈的是我的實際代碼,它返回的字符串數據和轉換爲整數的擴展名我沒有發佈,因爲有一個清楚的理解,誰是假設回答我的問題的人。關於輸出我已經刪除了打印語句。 – user1564024