title: Adding data to ArrayList while reading data from Excel using POI Apache
body: <p>I am trying to read data from Excel sheets using POI Apache. The problem I am having is that I want to read the data of all cell of a row at the same time and store it in ArrayList of Type Class but the output is only cell by cell.</p>
  
  <p>Here is the class that open the excel sheet and read the data cell by cell.</p>
  
  <pre><code>package testing;
  
  import javax.swing.JFileChooser;
  import java.io.File;
  import java.io.FileInputStream;
  
  import java.io.FileOutputStream;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  import java.util.TreeMap;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.ss.usermodel.Sheet;
  import org.apache.poi.ss.usermodel.Workbook;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class ReadExcelDemo 
  {
      ArrayList&lt;Data&gt; list = new ArrayList&lt;&gt;();
      String path;
  
     public ReadExcelDemo(String path)
     {
         this.path = path;
  
          try
          {
              FileInputStream file = new FileInputStream(new File(path));
  
              //Create Workbook instance holding reference to .xlsx file
              XSSFWorkbook workbook = new XSSFWorkbook(file);
  
              //Get first/desired sheet from the workbook
              XSSFSheet sheet = workbook.getSheetAt(0);
  
               System.out.println("");
  
              //Iterate through each rows one by one
              Iterator&lt;Row&gt; rowIterator = sheet.iterator();
              while (rowIterator.hasNext()) 
              {
                  Row row = rowIterator.next();
                  //For each row, iterate through all the columns
                  Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
  
                  while (cellIterator.hasNext()) 
                  {
                      Cell cell = cellIterator.next();
                      //Check the cell type and format accordingly
                      switch (cell.getCellType()) 
                      {
                          case Cell.CELL_TYPE_NUMERIC:
                              System.out.print(cell.getNumericCellValue() + "\t");
  
                              break;
                          case Cell.CELL_TYPE_STRING:
                              System.out.print(cell.getStringCellValue() + "\t");
                              break;
                      }
                  }
  
  
                  System.out.println("");
              }
              file.close();
          } 
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Data Class</p>
  
  <pre><code>package testing;
  
  
  public class Data {
  
  
      int ID;
      String F_Name,L_Name;
  
  
  
  
      public Data(int ID, String F_Name, String L_Name) {
          this.ID = ID;
          this.F_Name = F_Name;
          this.L_Name = L_Name;
      }
  
  
      public int getID() {
          return ID;
      }
  
      public String getF_Name() {
          return F_Name;
      }
  
      public String getL_Name() {
          return L_Name;
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/WDbKU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WDbKU.png" alt="enter image description here"></a></p>
  
  <p>I want to add the cell data in Arraylist like this at a single time</p>
  
  <pre><code>List.add(new Data(1,"Amit","shukla"));
  </code></pre>
  
  <p>but the data the iterator return is one by one like first it outputs <strong>1</strong> then <strong>amit</strong> and then <strong>shukla</strong> which is really difficult to add to arraylist</p>
  
  <p>I tried so much to add data to ArrayList at a single line but I couldn't.  It would be really helpful if you guy help me solve this problem.</p>