title: Dynamically create Excel template using NPOI
body: <p>I have an Excel <code>.xlsx</code> template file that I need to dynamically adapt to my needs. It contains only the title row for now, and two of the columns are "placeholder" columns where I need to insert new columns to fit my needs.</p>
  
  <p>The title row might look like this:</p>
  
  <pre><code>FirstName | LastName | City | #Modules# | Remarks | #Experts# | Country | Phone
  </code></pre>
  
  <p>The two columns with the <code>#</code> are the "placeholder" values - depending on some data I get from the database, I need to replace those single columns with multiple columns - e.g. replace <code>#Modules#</code> with three columns, titled <code>M1</code>, <code>M2</code> and <code>M3</code>, and replace <code>#Experts#</code> with <code>Expert 1</code> and <code>Expert 2</code> - so in the end, my Excel should look something like this:</p>
  
  <pre><code>FirstName | LastName | City | M1 | M2 | M3 | Remarks | Expert 1 | Expert 2 | Country | Phone
  </code></pre>
  
  <p>I'm trying to achieve this with NPOI v2.2, but haven't been successful as of yet.</p>
  
  <p>Approach #1: I tried using the <code>IRow.CreateCell</code> call, but that seems to be <em>overwriting</em> existing columns, so when I find <code>#Modules#</code> and replace it with my actual module data, I end up having </p>
  
  <pre><code>FirstName | LastName | City | M1 | M2 | M3 | Country | Phone
  </code></pre>
  
  <p>and my placeholder for the experts is gone (overwritten by one of the concrete modules).</p>
  
  <p>Code:</p>
  
  <pre><code>XSSFWorkbook templateWorkbook = new XSSFWorkbook(templateFileName);
  
  ISheet sheet = templateWorkbook.GetSheetAt(0);
  IRow firstRow = sheet.GetRow(0);  // title row
  
  // find the #Modules# placeholder - works as expected
  ICell cellModule = firstRow.Cells.Find(x =&gt; x.StringCellValue == "#Modules#");
  int moduleIndex = cellModule.ColumnIndex;   
  
  List&lt;string&gt; listOfModule = new List&lt;string&gt;(new[] { "M1", "M2", "M3" });
  
  foreach (string module in listOfModule)
  {
      ICell newCell = firstRow.CreateCell(moduleIndex, CellType.String);
      newCell.SetCellValue(module);
  
      moduleIndex += 1;
  }
  </code></pre>
  
  <p>This overwrites the existing cells in the title row and thus "destroys" part of my template file.....</p>
  
  <p>Approach #2: I tried to use the <code>.Insert()</code> method for inserting a cell:</p>
  
  <pre><code>foreach (string module in listOfModule)
  {
      // create a new cell, set its value
      ICell newCell = new XSSFCell(firstRow as XSSFRow, new CT_Cell());
      newCell.SetCellValue(module);
  
      // insert new cell into title row
      firstRow.Cells.Insert(moduleIndex, newCell);
  
      moduleIndex += 1;
  }
  </code></pre>
  
  <p>but it this case, the cell doesn't seem to actually get <strong>inserted</strong>, my title row doesn't change at all and just stays as it was in the beginning. </p>
  
  <p>Any ideas?? The documentation for NPOI is a bit ...... lacking, to say the least..... </p>
  
  <p>Thanks!</p>