title: How can i increment the rows in excel using apache poi
body: <p>I am writing a program to keep track of employee seating using java and apache poi to write to a excel sheet. The program will prompt the user to enter the desk number, employee name(s), and the number of employees sitting at that desk. My problem is that the program only writes the last information that was entered by the user, erasing what was previously entered in that row. How can I keep what was entered in the first row of the excel sheet? I have it set on a loop where the user will keep being prompted for the seating information until the user enters EXIT. I want it to increment down a row every time new information is entered.  </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import java.util.*;
  import org.apache.poi.hssf.usermodel.*;
  import org.apache.poi.hssf.util.*; 
  import org.apache.poi.ss.usermodel.*;
  import org.apache.poi.ss.util.*;
  import java.io.*;
  
  public class EmployeeSeatingRep {
  
  public static void main(String[] args){
  
      String deskNumber = "";
      String employeeName = "";
      int empsAtDesk = 0;
      boolean keepRunning = true;
  
  
  //Blank workbook
      HSSFWorkbook workbook = new HSSFWorkbook();
  //Blank sheet
      HSSFSheet sheet = workbook.createSheet("Seating Details");
  
  //create heading
  
          Row rowHeading = sheet.createRow(0);
          rowHeading.createCell(0).setCellValue("Desk:");
          rowHeading.createCell(1).setCellValue("Employee(s)Name:");
          rowHeading.createCell(2).setCellValue("Number At Desk:");
  
  //Font size and style loop for my headers
  
          for(int i = 0; i &lt; 3; i++)
          {
              CellStyle stylerowHeading = workbook.createCellStyle();
              Font font = workbook.createFont();
              font.setBold(true);
              font.setFontName(HSSFFont.FONT_ARIAL);
              font.setFontHeightInPoints((short) 11);
              stylerowHeading.setFont(font);
              stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
              rowHeading.getCell(i).setCellStyle(stylerowHeading);
          }
      while(keepRunning){
          Scanner scnr = new Scanner(System.in);
  
          System.out.print("Enter desk number: ");
          deskNumber = scnr.nextLine();
          if(deskNumber.equals("EXIT"))
          {
              System.out.print("You have ended the program");
              System.exit(0);
          }
          else
          {
              System.out.print("Enter employee name: ");
              employeeName = scnr.nextLine();
              System.out.print("Enter amount of employees at desk: ");
              empsAtDesk = scnr.nextInt();
  
  
  
  
  
  
  
  //This data needs to be written (Object[])
          Map &lt;String, Object[]&gt; data = new TreeMap&lt;String, Object[]&gt;();
          data.put("2", new Object[] {deskNumber, employeeName, empsAtDesk});
  
  //Iterate over data and write to sheet
          Set&lt;String&gt; keyset = data.keySet();
          int rownum = 1;
          for(String Key : keyset)
          {
              Row row = sheet.createRow(rownum++);
              Object [] objArr = data.get(Key);
              int cellnum = 0;
              for(Object obj : objArr)
              {
                  Cell cell = row.createCell(cellnum++);
                  if(obj instanceof String)
                  {
                      cell.setCellValue((String)obj);
                  }
                  else if(obj instanceof Integer)
                  {
                      cell.setCellValue((Integer)obj);
                  }
              }
              if(!(deskNumber.equals("EXIT")))
              {
                  rownum++;
  
          }
  
  //Auto size my columns that will be filled out with user input info.            
          for (int i = 0; i &lt; 3; i++)
          {
              sheet.autoSizeColumn(i);
          }
          }
  try{
  
   //save to excel file
          FileOutputStream out = new FileOutputStream(new File("Employee Seating Report.xls"));
          workbook.write(out);
          out.flush();
          out.close();
          System.out.println("Excel Written Succesfully...");
      } catch (FileNotFoundException e) {
  
          e.printStackTrace();
  
      } catch (IOException e){
  
          e.printStackTrace();
  
      } catch (Exception e) {
  
          System.out.println(e.getMessage());
      }
    } 
  }       
  }//pub static void end brace    
  }//pub class end brace
  </code></pre>