有關Cell[]溢出的問題

 

       今天遇到了一個有關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不一樣。

因而有時會出現數組溢出的錯誤。感覺很蹊蹺。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章