private void EndPackage() { string workbookPath = "/xl/workbook.xml"; string stylesheetPath = "/xl/styles.xml"; string sharedStringsPath = "/xl/sharedStrings.xml"; var idCounter = _WorksheetInfos.Count + 1; int ridWb = idCounter++; int ridStyles = idCounter++; int ridSharedStrings = idCounter++; var wbEntry = _Package.CreateStream(workbookPath, _CompressionLevel); using (var stream = wbEntry.Open()) { _Package.AddPackageRelationship(workbookPath, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId" + ridWb); _Package.AddContentType(workbookPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"); WriteWorkbookXml(stream); } var stylesEntry = _Package.CreateStream(stylesheetPath, _CompressionLevel); using (var stream = stylesEntry.Open()) { _Package.AddPartRelationship(workbookPath, stylesheetPath, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "rId" + ridStyles); _Package.AddContentType(stylesheetPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"); WriteStylesXml(stream); } var sharedStringsEntry = _Package.CreateStream(sharedStringsPath, _CompressionLevel); _Package.AddPartRelationship(workbookPath, sharedStringsPath, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings", "rId" + ridSharedStrings); _Package.AddContentType(sharedStringsPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"); using (var stream = sharedStringsEntry.Open()) using (var writer = new StreamWriter(stream)) { writer.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); writer.Write("<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"0\" uniqueCount=\"0\"></sst>"); } foreach (var ws in _WorksheetInfos) { _Package.AddPartRelationship(workbookPath, ws.Path, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rId" + ws.Id); _Package.AddContentType(ws.Path, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"); } _Package.CommitRelationships(_CompressionLevel); _Package.CommitContentTypes(_CompressionLevel); _Package.Close(); _Package = null; }
public ExcelSpreadsheetWriter(Stream outputStream, CompressionLevel compressionLevel = CompressionLevel.Fastest) : base(outputStream ?? new MemoryStream()) { if (outputStream.GetType().FullName == "System.Web.HttpResponseStream") { outputStream = new WriteStreamWrapper(outputStream); } _Package = new PackageWriteStream(outputStream); _CompressionLevel = compressionLevel; }