-3

我正在尝试编写从 excel 文件中读取数据的代码。excel 文件有 7 列,前 5 列由字符串类型数据组成,后 2 列由整数类型数据组成。现在我想将此数据发送到网页的形式。

我的代码有问题:我的代码将整个 excel 数据作为字符串返回,因为我只需要前 5 列作为字符串,最后 2 列作为整数。另外,虽然网页表单的所有字段都是文本字段,但我不确定为什么当我发送字符串数据时这些字段会抛出错误“格式无效”。您能否查看以下代码并帮助我解决此问题。

@Test public void 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;

}  
4

1 回答 1

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来,每一行都有一个实例,每个字段都有其正确的类型。

于 2013-08-21T00:33:20.557 回答