/// <summary> /// Saves the workbook and all its components to the package. /// For internal use only! /// </summary> protected internal void Save() // Workbook Save { // ensure we have at least one worksheet if (Worksheets.Count == 0) { throw new Exception("Workbook Save Error: the workbook must contain at least one worksheet!"); } #region Delete calcChain component // if the calcChain component exists, we should delete it to force Excel to recreate it // when the spreadsheet is next opened if (_xlPackage.Package.PartExists(_uriCalcChain)) { // there will be a relationship with the workbook, so first delete the relationship var calcChain = new Uri("calcChain.xml", UriKind.Relative); foreach (var relationship in _xlPackage.Workbook.Part.GetRelationships()) { if (relationship.TargetUri == calcChain) { _xlPackage.Workbook.Part.DeleteRelationship(relationship.Id); break; } } // delete the calcChain component _xlPackage.Package.DeletePart(_uriCalcChain); } #endregion // save the workbook if (_xmlWorkbook != null) { _xlPackage.SavePart(WorkbookUri, _xmlWorkbook); _xlPackage.WriteDebugFile(_xmlWorkbook, "xl", "workbook.xml"); } // save the properties of the workbook if (_properties != null) { _properties.Save(); } // save the style sheet if (_xmlStyles != null) { _xlPackage.SavePart(StylesUri, _xmlStyles); _xlPackage.WriteDebugFile(_xmlStyles, "xl", "styles.xml"); } // save the shared strings if (_xmlSharedStrings != null) { _xlPackage.SavePart(SharedStringsUri, _xmlSharedStrings); _xlPackage.WriteDebugFile(_xmlSharedStrings, "xl", "sharedstrings.xml"); } // save all the open worksheets foreach (ExcelWorksheet worksheet in Worksheets) { worksheet.Save(); } }
/// <summary> /// Saves the office document properties back to the package (if they exist!). /// </summary> protected internal void Save() { if (_xmlPropertiesCore != null) { _xlPackage.WriteDebugFile(_xmlPropertiesCore, "docProps", "core.xml"); _xlPackage.SavePart(CorePropertiesUri, _xmlPropertiesCore); } if (_xmlPropertiesExtended != null) { _xlPackage.WriteDebugFile(_xmlPropertiesExtended, "docProps", "app.xml"); _xlPackage.SavePart(ExtendedPropertiesUri, _xmlPropertiesExtended); } if (_xmlPropertiesCustom != null) { _xlPackage.WriteDebugFile(_xmlPropertiesCustom, "docProps", "custom.xml"); _xlPackage.SavePart(CustomPropertiesUri, _xmlPropertiesCustom); } }
/// <summary> /// Saves the worksheet to the package. For internal use only. /// </summary> internal void Save() // Worksheet Save { #region Delete the printer settings component (if it exists) // we also need to delete the relationship from the pageSetup tag var pageSetup = WorksheetXml.SelectSingleNode("//d:pageSetup", NameSpaceManager); if (pageSetup != null) { var attr = (XmlAttribute)pageSetup.Attributes.GetNamedItem("id", ExcelPackage.schemaRelationships); if (attr != null) { var relID = attr.Value; // first delete the attribute from the XML pageSetup.Attributes.Remove(attr); // get the URI var relPrinterSettings = Part.GetRelationship(relID); var printerSettingsUri = new Uri("/xl" + relPrinterSettings.TargetUri.ToString().Replace("..", ""), UriKind.Relative); // now delete the relationship Part.DeleteRelationship(relPrinterSettings.Id); // now delete the part from the package xlPackage.Package.DeletePart(printerSettingsUri); } } #endregion // save the header & footer (if defined) if (_headerFooter != null) { HeaderFooter.Save(); } // replace the numeric Cell IDs we inserted with AddNumericCellIDs() ReplaceNumericCellIDs(); // save worksheet to package var partPack = xlPackage.Package.GetPart(WorksheetUri); WorksheetXml.Save(partPack.GetStream(FileMode.Create, FileAccess.Write)); xlPackage.WriteDebugFile(WorksheetXml, @"xl\worksheets", "sheet" + SheetID + ".xml"); //clear field _worksheetXml becouse need rebuild column number tag in columns _worksheetXml = null; }