Пример #1
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (VbaProject == null)
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    ChangeContentTypeWorkbook(ExcelPackage.contentTypeWorkbookDefault);
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    ChangeContentTypeWorkbook(ExcelPackage.contentTypeWorkbookMacroEnabled);
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
            }

            UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (VbaProject != null)
            {
                VbaProject.Save();
            }
        }
Пример #2
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (_vba == null && !_package.Package.PartExists(new Uri(ExcelVbaProject.PartUri, UriKind.Relative)))
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookDefault;
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookMacroEnabled;
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
                worksheet.Part.SaveHandler = worksheet.SaveHandler;
            }

            // Issue 15252: save SharedStrings only once
            Packaging.ZipPackagePart part;
            if (_package.Package.PartExists(SharedStringsUri))
            {
                part = _package.Package.GetPart(SharedStringsUri);
            }
            else
            {
                part = _package.Package.CreatePart(SharedStringsUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", _package.Compression);
                Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings");
            }

            part.SaveHandler = SaveSharedStringHandler;
            //UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (_vba != null)
            {
                VbaProject.Save();
            }
        }
Пример #3
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (VbaProject == null)
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookDefault;
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookMacroEnabled;
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
                worksheet.Part.SaveHandler = worksheet.SaveHandler;
            }

            var part = _package.Package.CreatePart(SharedStringsUri, ExcelPackage.contentTypeSharedString, _package.Compression);

            part.SaveHandler = SaveSharedStringHandler;
            Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings");
            //UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (_vba != null)
            {
                VbaProject.Save();
            }
        }