title: Export to Excel using Open XML. But Last cell not filling up
body: <p>I used <a href="http://openxmldeveloper.org/forums/permalink/123327/8373/ShowThread.aspx#8373" rel="nofollow noreferrer">this code snippet</a> to export data to Excel using Open XML.</p>
  
  <p>The code was in VB.Net so I converted it to C#. It's working fine with only one (annoying) glitch. The last cell (Z5) is blank. When I used the VB.Net version it's filling all the cells. I manually compared the C# vs the VB.Net code but it's functionally similar byte by byte. But still it's not able to fill up the last cell. Any idea why?</p>
  
  <p>C# version of the code is below for your reference:</p>
  
  <pre><code>protected void Export()
  {
      Response.ClearHeaders();
      Response.ClearContent();
      Response.Clear();
      Response.Buffer = true;
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
      //"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" '"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" '"application/vnd.ms-excel"
      Response.AddHeader("content-disposition", "attachment; filename=Test.xlsx");
      Response.Charset = "";
      this.EnableViewState = false;
      MemoryStream ms = new MemoryStream();
      SpreadsheetDocument objSpreadsheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook);
      WorkbookPart objWorkbookPart = objSpreadsheet.AddWorkbookPart();
      objWorkbookPart.Workbook = new Workbook();
      WorksheetPart objSheetPart = objWorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
      objSheetPart.Worksheet = new Worksheet(new SheetData());
      Sheets objSheets = objSpreadsheet.WorkbookPart.Workbook.AppendChild&lt;Sheets&gt;(new Sheets());
      Sheet objSheet = new Sheet();
      objSheet.Id = objSpreadsheet.WorkbookPart.GetIdOfPart(objSheetPart);
      objSheet.SheetId = 1;
      objSheet.Name = "mySheet";
      objSheets.Append(objSheet);
  
      for (int intRow = (int)('A'); intRow &lt;= (int)('Z'); intRow++)
      {
          for (uint intCol = 1; intCol &lt;= 5; intCol++)
          {
              Cell objCell = InsertCellInWorksheet(Convert.ToString((char)intRow), intCol, objSheetPart);
              objCell.CellValue = new CellValue("This was a test: " + Convert.ToString((char)intRow) + intCol.ToString());
              objCell.DataType = new EnumValue&lt;CellValues&gt;(CellValues.String);
              objSpreadsheet.WorkbookPart.Workbook.Save();
          }
      }
  
  
      objSpreadsheet.WorkbookPart.Workbook.Save();
      objSpreadsheet.Close();
      ms.WriteTo(Response.OutputStream);
      Response.Flush();
      Response.End();
  }
  
  private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
  {
      Worksheet worksheet = worksheetPart.Worksheet;
      var sheetData = worksheet.GetFirstChild&lt;SheetData&gt;();
      string cellReference = columnName + rowIndex;
  
      // If the worksheet does not contain a row with the specified row index, insert one.
      Row row;
      if (sheetData.Elements&lt;Row&gt;().Where(r =&gt; r.RowIndex == rowIndex).Count() != 0)
      {
          row = sheetData.Elements&lt;Row&gt;().Where(r =&gt; r.RowIndex == rowIndex).First();
      }
      else
      {
          row = new Row { RowIndex = rowIndex };
          sheetData.Append(row);
      }
  
      // If there is not a cell with the specified column name, insert one.  
      if (row.Elements&lt;Cell&gt;().Where(c =&gt; c.CellReference.Value == columnName + rowIndex).Count() &gt; 0)
      {
          return row.Elements&lt;Cell&gt;().Where(c =&gt; c.CellReference.Value == cellReference).First();
      }
      // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
      Cell refCell = row.Elements&lt;Cell&gt;().FirstOrDefault(cell =&gt; string.Compare(cell.CellReference.Value, cellReference, true) &gt; 0);
  
      var newCell = new Cell { CellReference = cellReference };
      row.InsertBefore(newCell, refCell);
  
      worksheet.Save();
      return newCell;
  }
  </code></pre>