private void BuildResourceSheet(SpreadsheetDocument doc, string tabName) { if (doc == null) throw new ArgumentNullException("doc"); var sheets = doc.WorkbookPart.Workbook.Sheets ?? doc.WorkbookPart.Workbook.AppendChild(new Sheets()); //Use the existing sheet in the template Sheet sheet; WorksheetPart worksheetPart; SheetData sheetData; if (!doc.TryGetFirstSheetByName(tabName, out sheet)) { //Create new sheet if not in the tempalte worksheetPart = doc.WorkbookPart.AddNewPart<WorksheetPart>(); var sheetId = 9999; var rows = AddResourceColumns(doc, tabName); sheetData = new SheetData(rows); worksheetPart.Worksheet = new Worksheet(sheetData); worksheetPart.Worksheet.AddMdsolNamespaceDeclaration(); sheet = new Sheet { Id = doc.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = (uint) sheetId, Name = tabName }; sheets.Append(sheet); } else { //Get the existing sheet in the template worksheetPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id); var rows = AddResourceColumns(doc, tabName); sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); sheetData.Append(rows); worksheetPart.Worksheet.AddMdsolNamespaceDeclaration(); } }