有关Cell[]溢出的问题

来源:互联网 发布:mysql字符串怎么索引 编辑:程序博客网 时间:2024/05/01 21:03
 

       今天遇到了一个有关Excel文件导入到数据库时Cell[]的长度不固定的问题。

部分代码如下

for (int i = 0; i < sheets.length; ++i) {
    System.out.println("sheet getRows: " + sheets[i].getRows());
    for (int row = 1; row < sheets[i].getRows(); ++row) {
     Cell[] cells = sheets[i].getRow(row);

     System.out.println("CellsLength "+cells.length);
     String userId = cells[0].getContents();
     User user = userManager.getUserById(userId);
     System.out.println("userID: " + userId);
     if (user != null) {
      continue;
     } else {
      System.out.println("已加入!");
      user = new User();
     }

     String longinId = cells[1].getContents();
     if (longinId != null && !"".equals(longinId.trim())) {
      user.setLoginid(longinId);
     } else {
      System.out.println("已加入!");

      ActionMessage msg = new ActionMessage(
        "errors.longinIdNULL");
      errors.add(ActionErrors.GLOBAL_ERROR, msg);
      saveMessages(request,errors);
      //this.saveDirectlyMessage(request, msg);

     }

     String passwd = cells[2].getContents();
     if (passwd != null && !"".equals(passwd.trim())) {
      user.setPasswd(passwd);
     } else {
      System.out.println("已加入!");
      // 出错
//      ActionMessage msg = new ActionMessage(
//        "errors.passwdNULL");
//      errors.add(ActionErrors.GLOBAL_ERROR, msg);
     }

     String name = cells[3].getContents();
     if (name != null && !"".equals(name.trim())) {
      user.setName(name);
     } else {
      System.out.println("已加入!");
      // 出错
//      ActionMessage msg = new ActionMessage("errors.nameNULL");
//      errors.add(ActionErrors.GLOBAL_ERROR, msg);
     }

     String gender = cells[4].getContents();
     if (gender != null && !"".equals(gender.trim())) {
      // 验证是Byte
      user.setGender(Byte.valueOf(gender));
     } else {
     }

     String identitycard = cells[5].getContents();
     if (identitycard != null && !"".equals(identitycard.trim())) {
      user.setIdentitycard(identitycard);
     } else {
     }

     String unitId = cells[6].getContents();
     if (unitId != null && !"".equals(unitId.trim())) {
      // 验证为整形
      UnitInfo unitInfo = unitManager.getUnitByID(Integer
        .valueOf(unitId));
      user.setUnitInfo(unitInfo);
     } else {
//      ActionMessage msg = new ActionMessage(
//        "errors.unitIDNULL");
//      errors.add(ActionErrors.GLOBAL_ERROR, msg);
     }

     String telephone = cells[7].getContents();
     if (telephone != null && !"".equals(telephone.trim())) {
      user.setTelephone(telephone);
     } else {
     }

     String email = cells[8].getContents();
     if (email != null && !"".equals(email.trim())) {
      // 验证格式
      user.setEmail(email);
     } else {
     }

     String descn = cells[9].getContents();
     if (descn != null && !"".equals(descn.trim())) {
      user.setDescn(descn);
     } else {
     }

     String comment = cells[10].getContents();
     if (comment != null && !"".equals(comment.trim())) {
      user.setComment(comment);
     } else {
     }

 

}

    其中因为在Excel文件中部分数据段时为空的,使得每次读出一行(对应一个用户信息)cells.length不一样。

因而有时会出现数组溢出的错误。感觉很蹊跷。

原创粉丝点击