java - Read data from excel and Write to List -


i want data list , display it.but out put display 4 times. excel file containing 4 data. want 1 record. code is

public static list readdatafromexcel() throws ioexception{         string filename = "path";          list sheetdata = new arraylist();          fileinputstream fis = null;         try {              fis = new fileinputstream(filename);              xssfworkbook workbook = new xssfworkbook(fis);             xssfsheet sheet = workbook.getsheetat(0);              iterator rows = sheet.rowiterator();             while (rows.hasnext()) {                 xssfrow row = (xssfrow) rows.next();                 iterator cells = row.celliterator();                  list data = new arraylist();                 while (cells.hasnext()) {                     xssfcell cell = (xssfcell) cells.next();                     string value=" ";                     switch (cell.getcelltype())                      {                         case cell.cell_type_numeric:                             value = bigdecimal.valueof(cell.getnumericcellvalue()).toplainstring();                             data.add(value);                             break;                         case cell.cell_type_string:                             value=cell.getstringcellvalue();                             data.add(value);                             break;                         case cell.cell_type_blank:                             value = " ".tostring();                             data.add(value);                             break;                         case cell.cell_type_boolean:                             value = boolean.valueof(cell.getbooleancellvalue()).tostring();                             data.add(value);                             break;                     }                      sheetdata.add(data);                  }                  fis.close();             }         } catch (ioexception e) {             e.printstacktrace();         }     return sheetdata;     } 

main methods

public static void main(string[] args) throws ioexception {      list serverdetailslist = readdatafromexcel.readdatafromexcel();     list oneserverdetailslist = new arraylist();          (int = 0; < serverdetailslist.size(); i++) {              system.out.println(serverdetailslist.get(i));          }      } 

out put image enter image description here

excel screnshot enter image description here

i see adding inner arraylist(data) main arraylist (sheetdata) many number of times find cell.

the approach should have been

 while (rows.hasnext()) {             xssfrow row = (xssfrow) rows.next();             iterator cells = row.celliterator();              list data = new arraylist();           while (cells.hasnext()) {                 xssfcell cell = (xssfcell) cells.next();                 string value=" ";                 switch (cell.getcelltype())                  {                     case cell.cell_type_numeric:                       value =                bigdecimal.valueof(cell.getnumericcellvalue()).toplainstring();                         data.add(value);                         break;                     case cell.cell_type_string:                         value=cell.getstringcellvalue();                         data.add(value);                         break;                     case cell.cell_type_blank:                         value = " ".tostring();                         data.add(value);                         break;                     case cell.cell_type_boolean:                         value = boolean.valueof(cell.getbooleancellvalue()).tostring();                         data.add(value);                         break;                 }                  //sheetdata.add(data);              }        sheetdata.add(data);       fis.close();       } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -