Beispiel #1
0
            // ReSharper disable once MemberHidesStaticFromOuterClass
            internal static void Save(Workbook workbook, Stream outputStream, bool compress)
            {
                var relationshipCounter = new RelationshipCounter();
                var package             = new XlsxPackage();

                // Must be done before calling GetXlsxStyles as it may add styles
                foreach (var sheet in workbook.Sheets)
                {
                    HandleLargeNumbers(sheet);
                }

                ExtractWorkbookSpecialXmlParts(workbook, out var styles, out var ignoredErrors);
                var sharedStrings = new SharedStrings();

                // docProps/core.xml
                var cp = CreateCoreFileProperties(workbook, relationshipCounter);

                package.XmlFiles.Add(cp.Target);
                package.Relationships.Add(cp);

                // xl/styles.xml
                var stylesXml = StyleWriter.CreateStyleXml(styles);
                var stylesRel = new Relationship(relationshipCounter)
                {
                    Type   = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
                    Target = stylesXml
                };

                package.XmlFiles.Add(stylesXml);
                package.WorkbookRelationships.Add(stylesRel);

                // xl/worksheets/sheetX.xml
                var sheetPackageInfos = new List <SheetPackageInfo>();
                var i = 0;

                foreach (var sheet in workbook.Sheets)
                {
                    i++;
                    var rel = CreateSheetFile(sheet, i, relationshipCounter, styles, ignoredErrors[sheet],
                                              sharedStrings, out var sheetRels);
                    if (sheetRels != null)
                    {
                        package.XmlFiles.Add(sheetRels);
                    }

                    package.XmlFiles.Add(rel.Target);
                    package.WorkbookRelationships.Add(rel);

                    var sheetPackageInfo = new SheetPackageInfo
                    {
                        RelationshipId = rel.Id,
                        SheetId        = i,
                        SheetName      = sheet.Name
                    };
                    if (sheet.PageSetup.PrintRepeatColumns > 0)
                    {
                        sheetPackageInfo.RepeatCols = "'" + sheet.Name + "'!$A:$" +
                                                      CellAddressHelper.ColToReference(sheet.PageSetup.PrintRepeatColumns - 1);
                    }

                    if (sheet.PageSetup.PrintRepeatRows > 0)
                    {
                        sheetPackageInfo.RepeatRows = "'" + sheet.Name + "'!$1:$" + sheet.PageSetup.PrintRepeatRows;
                    }

                    sheetPackageInfos.Add(sheetPackageInfo);
                }

                // xl/sharedStrings.xml
                if (sharedStrings.Count > 0)
                {
                    var ssx = sharedStrings.ToRelationship(relationshipCounter);
                    package.XmlFiles.Add(ssx.Target);
                    package.WorkbookRelationships.Add(ssx);
                }

                // xl/workbook.xml
                var wb = CreateWorkbookFile(sheetPackageInfos, relationshipCounter);

                package.XmlFiles.Add(wb.Target);
                package.Relationships.Add(wb);

                // xl/_rels/workbook.xml.rels
                package.SaveToStream(outputStream, compress);
            }
            internal void Save(Stream outputStream)
            {
                // docProps/core.xml
                var cp = CreateCoreFileProperties();

                _package.XmlFiles.Add(cp.Target);
                _package.Relationships.Add(cp);

                // xl/styles.xml
                var styles    = StyleWriter.CreateStyleXml(_styles);
                var stylesRel = new Relationship(_relationshipCounter)
                {
                    Type   = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
                    Target = styles
                };

                _package.XmlFiles.Add(styles);
                _package.WorkbookRelationships.Add(stylesRel);

                // xl/worksheets/sheetX.xml
                var sheetinfos = new List <SheetPackageInfo>();
                int i          = 0;

                foreach (var sheet in _workbook.Sheets)
                {
                    i++;
                    XmlFile sheetRels;
                    var     rel = CreateSheetFile(sheet, i, out sheetRels);
                    if (sheetRels != null)
                    {
                        _package.XmlFiles.Add(sheetRels);
                    }

                    _package.XmlFiles.Add(rel.Target);
                    _package.WorkbookRelationships.Add(rel);

                    var sheetinfo = new SheetPackageInfo
                    {
                        RelationshipId = rel.Id,
                        SheetId        = i,
                        SheetName      = sheet.Name
                    };
                    if (sheet.PageSetup.PrintRepeatColumns > 0)
                    {
                        sheetinfo.RepeatCols = "'" + sheet.Name + "'!$A:$" + CellAddressHelper.ColToReference(sheet.PageSetup.PrintRepeatColumns - 1);
                    }
                    if (sheet.PageSetup.PrintRepeatRows > 0)
                    {
                        sheetinfo.RepeatRows = "'" + sheet.Name + "'!$1:$" + sheet.PageSetup.PrintRepeatRows;
                    }

                    sheetinfos.Add(sheetinfo);
                }

                // xl/sharedStrings.xml
                if (_sharedStrings.Count > 0)
                {
                    var ssx = _sharedStrings.ToRelationship(_relationshipCounter);
                    _package.XmlFiles.Add(ssx.Target);
                    _package.WorkbookRelationships.Add(ssx);
                }

                // xl/workbook.xml
                var wb = CreateWorkbookFile(sheetinfos);

                _package.XmlFiles.Add(wb.Target);
                _package.Relationships.Add(wb);

                // xl/_rels/workbook.xml.rels
                _package.SaveToStream(outputStream);
            }