title: What is the easiest way to count .xlsx workbook sheets using c#, NPOI and an XSSF workbook?
body: <p>I am trying to count the number of sheets in a workbook. The workbook is created using NPOI and there doesn't seem to be a way to count the amount of sheets using the C# version of NPOI?</p>
  
  <p>This is a really tricky thing to both explain and show... But I will give it a try.</p>
  
  <p>What I am trying to do is having an existing excel-file as a template for statistics. This existing excel-file can have different amounts of templates and I need to be able to count these templates to know where to place my new sheets and edit their names.</p>
  
  <p>The sender of the data only has to chose which template-sheet should be filled with which data, and I will then remove the template-sheets from the workbook after all data has been inserted.</p>
  
  <p>What I have tried:</p>
  
  <p>I have read the documentation and searched for information and have tried the following approaches:</p>
  
  <ul>
  <li>getNumberOfSheets - <a href="https://stackoverflow.com/questions/2077566/how-to-know-number-of-sheets-in-a-workbook">How to know number of sheets in a workbook?</a></li>
  </ul>
  
  <p>Problem with this approach: The C# version of NPOI doesn't seem to have getNumberOfSheets.</p>
  
  <ul>
  <li>Convert found row-counters into sheet-counters -  <a href="https://stackoverflow.com/questions/31344841/npoi-get-excel-row-count-to-check-if-it-is-empty">NPOI - Get excel row count to check if it is empty</a></li>
  </ul>
  
  <p>Can't really recreate the code to work for sheets as the functionality for sheets and rows are too different.</p>
  
  <pre><code>var sheetIndex = 0;
  foreach (var sheet in requestBody.Sheets)
  {
      if (sheet.TemplateNumber == "")
      {
         sheetTemplate = templateWorkbook.CreateSheet(sheet.Name);
      }
      else
      {
          sheetTemplate = templateWorkbook.CloneSheet(Convert.ToInt32(sheet.SheetTemplate));
          if (!templates.Contains(Convert.ToInt32(sheet.SheetTemplate)))
          {
              templates.Add(Convert.ToInt32(sheet.SheetTemplate));
          }
  
  // Do math's to make sure we add the name to the newly created sheet further down the code (I need to actual index here)
      }
  // Insert statistics 
  
  //After inserting statistics:
      workingCopy.SetSheetName(sheetIndex, sheet.Name);
  
      foreach (var template in templates)
      {
          workingCopy.RemoveSheetAt(template);
      }
  }
  </code></pre>