问题描述
我想写code,从一个excel文件读取数据。 Excel文件有7列,第一列5串组成的数据类型和最后两列是由整数数据类型。现在我想这个数据发送到网页的形式。
的问题与我的code:我的code将退回整个Excel数据,在那里,因为我只需要第5列,字符串和最后2列作为整数字符串。此外,虽然网页形式的所有字段都是文本字段,我不知道为什么字段抛出一个错误无效格式当我发送的字符串数据。能否请你看看下面code和帮助我解决这个问题。
@Test
公共无效SampleCustNumFormat()抛出异常{
字符串vURL;
串vEmail;
串vPswd;
串vCnfmPswd;
INT vCustAccNum;
INT VZIP code;
长IWAIT;
INT大小;
从Excel //读取数据测试 字符串xlPath =C:\\\\ SCE文档\\\\ \\\\自动化InputData_Registration.xls
ArrayList的< CustomerData> customerData = getExcelData(xlPathAccountHolderRegistration);
System.setProperty(webdriver.chrome.driver,C:\\\\ chromedriver.exe);
司机=新ChromeDriver();
driver.manage()超时()implicitlyWait(30 TimeUnit.SECONDS)。
IWAIT = 3000; 对于(INT K = 1; K< xRows; k ++)
{
driver.navigate()至(vURL)。
。driver.findElement(By.linkText(注册))点击();
driver.findElement(By.id(emailInputBox_input))清()。
driver.findElement(By.id(emailInputBox_input))的SendKeys(vEmail)。
driver.findElement(By.id(pwdInputBox_input))清()。
driver.findElement(By.id(pwdInputBox_input))的SendKeys(vPswd)。
driver.findElement(By.id(confirmpwd_input))清()。
driver.findElement(By.id(confirmpwd_input))的SendKeys(vCnfmPswd)。
。driver.findElement(By.id(收音机1))点击();
driver.findElement(By.id(accountInputBox_input))的SendKeys(Integer.toString(vCustAccNum));
driver.findElement(By.id(accountInputBox_input))清()。
driver.findElement(By.id(ZIP codeBox_input))的SendKeys(Integer.toString(VZIP code))。
driver.findElement(By.id(ZIP codeBox_input))清()。
。driver.findElement(By.id(terms3))点击();
/ * driver.findElement(By.id(registerButton_label))点击();
。driver.findElement(By.id(continueButton_label))点击();
视频下载(IWAIT);
* / }
}//自定义的方法
公众的ArrayList< CustomerData> getExcelData(路径字符串,字符串shtName)
抛出异常{
ArrayList的< CustomerData> customerDataList =新的ArrayList< CustomerData>(); 文件myxl =新的文件(路径);
科幻的FileInputStream =新的FileInputStream(myxl);
HSSFWorkbook myWB =新HSSFWorkbook(FI);
HSSFSheet MySheet的工作= myWB.getSheet(shtName); xRows = mySheet.getLastRowNum()+ 1;
xCols = mySheet.getRow(0).getLastCellNum(); 的for(int i = 1; I< xRows;我++){
HSSFRow行= mySheet.getRow(I) CustomerData为CustomerDetails =新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.setvZip code((INT)row.getCell(6).getNumericCellValue());
customerDataList.add(为CustomerDetails);
的System.out.println(第2行:+ row.getCell(3));
// row.getCell(2).setCellValue(convertFormatCustNo(customerDetails.getCustNo()));
}
返回customerDataList;}
所有你必须正确地设计课程,以反映的数据模型,你正在处理,而不是把一切都为String。首先
您可以简单地创建一个反映一列在Excel表类:
公共类账户持有
{
私人字符串ID;
私人字符串URL;
私人字符串电子邮件;
私人字符串密码;
私人字符串confirmPassword;
私人诠释了accountNumber;
私人诠释拉链code; 公共账户持有()
{
} 公共字符串的getId()
{
返回ID;
} 公共无效SETID(ID)
{
this.id = ID;
} // TODO:公共getter和setter做其余字段以同样的方式}
然后改变 getExcelData()
返回一个账户持有[]
,而不仅仅是一堆字符串
的对象。
最后修改 getExcelData()
中的循环:
账户持有[] =账户持有新账户持有[xRows]的for(int i = 0; I< xRows;我++)
{
HSSFRow行= mySheet.getRow(I) 账户持有人账户持有=新账户持有();
accountHolder.setId(row.getCell(0).getStringCellValue()); // TODO:账户持有的字符串其余字段 accountHolder.setAccountNumber(row.getCell(5).getNumericCellValue());
accountHolder.setZip code(row.getCell(6).getNumericCellValue()); 账户持有[I] =账户持有;
}返回账户持有;
这样,你有每一行的账户持有
实例,具有它的正确类型的每个字段。
I am trying to write code that reads data from an excel file. The excel file has 7 columns and the first 5 columns consists of string type data and the last 2 columns consists of integer type data. Now I want to send this data to a form of a web page.
Problem with my code: My code is returning the entire excel data as strings where as I need only the first 5 columns as strings and the last 2 columns as integers. Also, though all the fields of a form of webpage are text fields, I am not sure why the fields are throwing an error " Invalid format" when I sent the string data. Could you please look into the below code and help me in resolving this issue.
@Test public void SampleCustNumFormat() throws Exception {
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;
}
First of all you have to design your classes properly to reflect the data model you are dealing with, rather than treating everything as String.
You can simply create a class that reflects one row in your excel sheet:
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
}
Then change getExcelData()
to return an AccountHolder[]
rather than just a bunch of String
objects.
Finally modify getExcelData()
in the loop:
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;
That way you have an AccountHolder
instance for each row, with each field having its proper type.
这篇关于请求解决有关发送整数的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!