title: Delete sheet from WorkBook in POI
body: <p>I have a WorkBook and I'm trying to delete sheets which names don't match with a specific String.</p>
  
  <p>Here is my code</p>
  
  <pre><code>XSSFWorkbook book = new XSSFWorkbook(new FileInputStream(excelName));                   
              for(int i=0;i&lt;book.getNumberOfSheets();i++){
                  System.out.println(book.getSheetAt(i).getSheetName());
                  if(!book.getSheetAt(i).getSheetName().equals(sheetName)){
                      book.removeSheetAt(i);
                  }
              }   
  </code></pre>
  
  <p>The code runs well, but it doesn't perform the desired task</p>
  
  <h2>EDIT</h2>
  
  <p>The solution as said is to reverse the loop order. Here is a cleaner code</p>
  
  <pre><code>private void removeOtherSheets(String sheetName, XSSFWorkbook book) {       
              for(int i=book.getNumberOfSheets()-1;i&gt;=0;i--){
                  XSSFSheet tmpSheet =book.getSheetAt(i);
                  if(!tmpSheet.getSheetName().equals(sheetName)){
                      book.removeSheetAt(i);
                  }
              }       
      }
  </code></pre>