title: Insert a new worksheet into a spreadsheet document OpenXml
body: <p>What im trying to do is create multiple worksheets within a work book using datasets the code i have to create a sheet data object from a data set is:</p>
  
  <pre><code>        public static SheetData CreateDataSheet(DataSet ds)
      {
          var xlSheetData = new SheetData();
          if (ds.Tables.Count &gt; 0 &amp;&amp; ds.Tables[0].Rows.Count &gt; 0)
          {
              var tbl = ds.Tables[0];
              foreach (DataRow row in tbl.Rows)
              {
                  var xlRow = new Row();
                  foreach (DataColumn col in tbl.Columns)
                  {
                      var cellData = row[col];
                      Cell xlCell = null;
                      if (cellData != null)
                      {
                          xlCell = new Cell(new InlineString(new Text(cellData.ToString())))
                          {
                              DataType = CellValues.InlineString
                          };
                      }
                      else
                      {
                          xlCell = new Cell(new InlineString(new Text(String.Empty)))
                          {
                              DataType = CellValues.InlineString
                          };
                      }
                      xlRow.Append(xlCell);
                  }
                  xlSheetData.Append(xlRow);
              }
          }
          return xlSheetData;
      }
  </code></pre>
  
  <p>Then to create the spreadsheet and append the above to the spreadsheet i have:</p>
  
  <pre><code> public static void CreateSpreadsheetWorkbook(string filepath, List&lt;SheetData&gt; sd)
      {
  
  
          var spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
          var workbookpart = spreadsheetDocument.AddWorkbookPart();
          workbookpart.Workbook = new Workbook();
  
  
          foreach (var x in sd)
          {
              var newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
              newWorksheetPart.Worksheet = new Worksheet(x);
              var sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild&lt;Sheets&gt;(new Sheets());
  
              uint sheetId = 1;
              if (sheets.Elements&lt;Sheet&gt;().Any())
              {
                  sheetId = sheets.Elements&lt;Sheet&gt;().Select(s =&gt; s.SheetId.Value).Max() + 1;
              }
  
              var sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart), SheetId = sheetId, Name = "mySheet" + sheetId };
              sheets.Append(sheet);
              workbookpart.Workbook.Save();
  
          }
  
          spreadsheetDocument.Close();
  
      }
  </code></pre>
  
  <p>This runs with out any errors however when i come to open the document its unable to be opened because it is corrupt.</p>
  
  <p>EDIT - final working version:</p>
  
  <pre><code>public static void CreateSpreadsheetWorkbook(string filepath, List&lt;SheetData&gt; sd)
      {
  
  
          var spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
          var workbookpart = spreadsheetDocument.AddWorkbookPart();
          workbookpart.Workbook = new Workbook();
  
          var sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild&lt;Sheets&gt;(new Sheets());
  
          foreach (var x in sd)
          {
              var newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
              newWorksheetPart.Worksheet = new Worksheet(x);
  
              sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild&lt;Sheets&gt;();
  
              uint sheetId = 1;
              if (sheets.Elements&lt;Sheet&gt;().Any())
              {
                  sheetId = sheets.Elements&lt;Sheet&gt;().Select(s =&gt; s.SheetId.Value).Max() + 1;
              }
  
              var sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart), SheetId = sheetId, Name = "mySheet" + sheetId };
              sheets.Append(sheet);
              workbookpart.Workbook.Save();
  
          }
  
          spreadsheetDocument.Close();
  
      }
  </code></pre>