title: How to add rows/cells in DocumentFormat.OpenXml Spreadsheet?
body: <p>I have a program which needs to take in a set of data, and write it out to a new XLSX spreadsheet. I'm currently using the <strong>DocumentFormat.OpenXml</strong> package from NuGet. The program is running fine to completion, not receiving any errors. However when I check the XLSX file, the workbook exists, the worksheet exists, and the worksheet has gotten the name assigned to it in code. </p>
  
  <p>BUT... none of the rows/cell data has actually been written to the file. All the cells in the worksheet are empty.</p>
  
  <p>Here is my current code that does the actual XLSX stuff...<br>
  (file, being passed to SpreadsheetDocument.Create is a string, with the full path.)</p>
  
  <pre><code>using(SpreadsheetDocument doc = SpreadsheetDocument.Create(file, SpreadsheetDocumentType.Workbook))
  {
      WorkbookPart wbp = doc.AddWorkbookPart();
      wbp.Workbook = new Workbook();
      WorksheetPart wsp = wbp.AddNewPart&lt;WorksheetPart&gt;();
      wsp.Worksheet = new Worksheet(new SheetData());
      var ws = wsp.Worksheet;
      Sheets sheets = wbp.Workbook.AppendChild(new Sheets());
      Sheet sheet = new Sheet() { Id = wbp.GetIdOfPart(wsp), SheetId = 1, Name = batchinfo.Field&lt;string&gt;("NAME") };
      sheets.Append(sheet);
  
      if(!exhdr) 
      {
          var row = ws.AppendChild(new Row());
          foreach(DataRow dr in hdrs) 
          {
              var cell = row.AppendChild(new Cell());
              cell.CellValue = new CellValue(dr.Field&lt;string&gt;("COLHEADER"));
              cell.DataType = new EnumValue&lt;CellValues&gt;(CellValues.String);
          }
      }
  
      var loans = (from DataRow dr in data.Rows
                   group dr by dr.Field&lt;long&gt;("L_ROWID") into g
                   select new { LoanId = g.Key, Rows = g.OrderBy(r =&gt; r.Field&lt;double&gt;("ORDERIDX")).ToList()}
                   );
  
      foreach(var loan in loans) 
      {
          var row = ws.AppendChild(new Row());
          foreach (DataRow dr in loan.Rows)
          {
              var cell = row.AppendChild(new Cell());
              cell.CellValue = new CellValue(dr.Field&lt;string&gt;("DATAOUT"));
              cell.DataType = new EnumValue&lt;CellValues&gt;(CellValues.String);
          }
      }
      wbp.Workbook.Save();
  }
  </code></pre>
  
  <p>I have also tried modifying the code to create the row or cell object, assign all the properties, and THEN append it to the parent, like so... </p>
  
  <pre><code>var row = new Row();
  foreach (DataRow dr in loan.Rows) {
      var cell = new Cell();
      cell.CellValue = new CellValue(dr.Field&lt;string&gt;("DATAOUT"));
      cell.DataType = new EnumValue&lt;CellValues&gt;(CellValues.String);
      row.Append(cell);
  }
  ws.Append(row);
  </code></pre>
  
  <p>Any clue as to what I am doing wrong here?<br>
  Like I said, the file and the worksheet in it both seem to get created fine.<br>
  It's just the cells are all missing in action.</p>
  
  <p><strong>UPDATE:</strong> I saw in one example I found, there is also a <code>Save()</code> method on the Worksheet object. I added that in just before the <code>Workbook.Save()</code>, but it didn't make any difference.</p>
  
  <p>Or maybe there is a better library to use?</p>