public void Write(Workbook workbook) { /* Write workbook.xml * Write workbook.xml.rels * Write sheets.xml * Register all content types */ this.workbook = workbook; rels = new Relationships(); contentTypes = new List <object>(); WriteWorkbook(); //Write package relationships WriteRelationsips("_rels/.rels", rels); //Write [Content_Types].xml CT_Types ctypes = new CT_Types { Items = contentTypes.ToArray() }; WriteFile("[Content_Types].xml", ctypes, ContentTypesNs()); }
void WriteWorkbook() { rels.Add(new CT_Relationship { Type = Relationships.Workbook, Target = "xl/workbook.xml" }); OverrideContentType("/xl/workbook.xml", ContentTypes.Workbook); sharedStrings = new SharedStrings(); ClearStyles(); workbookRelationships = new Relationships(); int sheetIndex = 1; contentTypes.Add(new CT_Default { Extension = "xml", ContentType = ContentTypes.Xml }); contentTypes.Add(new CT_Default { Extension = "rels", ContentType = ContentTypes.Rels }); var sheets = new List <CT_Sheet>(); foreach (var sheet in workbook.Sheets) { var relativePath = String.Format("worksheets/sheet{0}.xml", sheetIndex); var sheetPath = "xl/" + relativePath; var rid = workbookRelationships.Add(new CT_Relationship { Target = relativePath, TargetMode = ST_TargetMode.Internal, Type = Relationships.Sheet }); OverrideContentType(sheetPath, ContentTypes.Sheet); var st = new CT_Sheet { id = rid, name = sheet.SheetName, sheetId = (uint)sheetIndex }; sheets.Add(st); WriteSheet(sheetPath, sheet); sheetIndex++; } var wb = new CT_Workbook() { sheets = sheets.ToArray(), bookViews = new[] { new CT_BookView() } }; var definedNames = new List <CT_DefinedName>(); foreach (var definedName in workbook.DefinedNames) { var dn = new CT_DefinedName { name = definedName.Name, Value = definedName.Value }; definedNames.Add(dn); } for (var i = 0; i < workbook.Sheets.Count; i++) { foreach (var definedName in workbook.DefinedNames) { var dn = new CT_DefinedName { name = definedName.Name, Value = definedName.Value, localSheetId = (uint)i, localSheetIdSpecified = true }; definedNames.Add(dn); } } if (definedNames.Count > 0) { wb.definedNames = definedNames.ToArray(); } WriteFile("xl/workbook.xml", wb, SpreadsheetNs(true)); if (sharedStrings.Count > 0) { WriteSharedStrings(); } if (!styles.Empty) { OverrideContentType("xl/styles.xml", ContentTypes.Styles); workbookRelationships.Add(new CT_Relationship { Target = "styles.xml", Type = Relationships.Styles }); WriteStyles("xl/styles.xml"); } WriteRelationsips("xl/_rels/workbook.xml.rels", workbookRelationships); }