public static SpreadsheetDocument Open(Package package, OpenSettings openSettings) { if (openSettings is null) { throw new ArgumentNullException(nameof(openSettings)); } if (openSettings.MarkupCompatibilityProcessSettings.ProcessMode != MarkupCompatibilityProcessMode.NoProcess && !openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions.Any()) { throw new ArgumentException(ExceptionMessages.InvalidMCMode); } var doc = new SpreadsheetDocument { OpenSettings = new OpenSettings(openSettings), }; doc.OpenCore(package); if (MainPartContentTypes[doc.DocumentType] != doc.MainPartContentType) { doc.UpdateDocumentTypeFromContentType(); } return(doc); }
/// <summary> /// Creates a new instance of the SpreadsheetDocument class from the IO stream. /// </summary> /// <param name="stream">The IO stream on which to open the SpreadsheetDocument.</param> /// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param> /// <param name="openSettings">The advanced settings for opening a document.</param> /// <returns>A new instance of SpreadsheetDocument.</returns> /// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception> /// <exception cref="IOException">Thrown when "stream" is not opened with Read (ReadWrite) access.</exception> /// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML SpreadsheetDocument.</exception> /// <exception cref="ArgumentException">Thrown when specified to process the markup compatibility but the given target FileFormatVersion is incorrect.</exception> public static SpreadsheetDocument Open(Stream stream, bool isEditable, OpenSettings openSettings) { if (openSettings is null) { throw new ArgumentNullException(nameof(openSettings)); } if (openSettings.MarkupCompatibilityProcessSettings.ProcessMode != MarkupCompatibilityProcessMode.NoProcess && !openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions.Any()) { throw new ArgumentException(ExceptionMessages.InvalidMCMode); } SpreadsheetDocument doc = new SpreadsheetDocument(); doc.OpenSettings = new OpenSettings(); doc.OpenSettings.AutoSave = openSettings.AutoSave; doc.OpenSettings.MarkupCompatibilityProcessSettings.ProcessMode = openSettings.MarkupCompatibilityProcessSettings.ProcessMode; doc.OpenSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions = openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions; doc.MaxCharactersInPart = openSettings.MaxCharactersInPart; doc.OpenCore(stream, isEditable); if (MainPartContentTypes[doc.DocumentType] != doc.MainPartContentType) { doc.UpdateDocumentTypeFromContentType(); } return(doc); }