title: How to add new sheets to existing excel workbook using apache POI?
body: <p>I am trying to write List data into multiple excel sheet in one work book. like for first list, the code will create new workbook and create new sheet for list[1], for second list it will create new sheet in existing workbook and so on. so i wrote below code. but it doesnt work and i am able to see only first sheet for list[1]. can someone help me to provide any alternate resolutions? </p>
  
  <p>the below code i have written </p>
  
  <pre><code>    ArrayList&lt;List&lt;String&gt;&gt; tempresultdata=this.getSummaryList();
      HSSFWorkbook workbook = new HSSFWorkbook();
      String fileName="Path\\To\\XLS";
      File file = new File(fileName);
      FileOutputStream out;           
      if(!file.exists()) // This will create new workbook with new sheet if it doesnt exists{
  
                  HSSFSheet mySheet = workbook.createSheet(sheetname);
                  writeExcel(mySheet,tempresultdata);
      } else // This add new sheet to above created workbook {
              try {
                  HSSFWorkbook myWorkBook = (HSSFWorkbook) WorkbookFactory.create(file);
                  workbook=myWorkBook;
                  HSSFSheet mySheet = (HSSFSheet) workbook.createSheet(sheetname);
                  writeExcel(mySheet,tempresultdata);                 
              } catch (InvalidFormatException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      }   
      try{
          out = new FileOutputStream(fileName,true);
          workbook.write(out);
          out.close();
          }catch(Exception e){ 
              e.printStackTrace();
          }
  </code></pre>
  
  <p>Thanks,
  Priyank Shah</p>