diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index b551198..006fe16 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -176,6 +176,7 @@ ws.Visibility = dSheet.State.Value.ToClosedXml(); var styleList = new Dictionary();// {{0, ws.Style}}; + PageSetupProperties pageSetupProperties = null; using (var reader = OpenXmlReader.Create(wsPart)) { @@ -233,11 +234,11 @@ else if (reader.ElementType == typeof(PageMargins)) LoadPageMargins((PageMargins)reader.LoadCurrentElement(), ws); else if (reader.ElementType == typeof(PageSetup)) - LoadPageSetup((PageSetup)reader.LoadCurrentElement(), ws); + LoadPageSetup((PageSetup)reader.LoadCurrentElement(), ws, pageSetupProperties); else if (reader.ElementType == typeof(HeaderFooter)) LoadHeaderFooter((HeaderFooter)reader.LoadCurrentElement(), ws); else if (reader.ElementType == typeof(SheetProperties)) - LoadSheetProperties((SheetProperties)reader.LoadCurrentElement(), ws); + LoadSheetProperties((SheetProperties)reader.LoadCurrentElement(), ws, out pageSetupProperties); else if (reader.ElementType == typeof(RowBreaks)) LoadRowBreaks((RowBreaks)reader.LoadCurrentElement(), ws); else if (reader.ElementType == typeof(ColumnBreaks)) @@ -1598,28 +1599,34 @@ ws.PageSetup.RowBreaks.Add(Int32.Parse(rowBreak.Id.InnerText)); } - private void LoadSheetProperties(SheetProperties sheetProperty, XLWorksheet ws) + private void LoadSheetProperties(SheetProperties sheetProperty, XLWorksheet ws, out PageSetupProperties pageSetupProperties) { + pageSetupProperties = null; if (sheetProperty == null) return; if (sheetProperty.TabColor != null) ws.TabColor = GetColor(sheetProperty.TabColor); - if (sheetProperty.OutlineProperties == null) return; - - if (sheetProperty.OutlineProperties.SummaryBelow != null) + if (sheetProperty.OutlineProperties != null) { - ws.Outline.SummaryVLocation = sheetProperty.OutlineProperties.SummaryBelow - ? XLOutlineSummaryVLocation.Bottom - : XLOutlineSummaryVLocation.Top; + + if (sheetProperty.OutlineProperties.SummaryBelow != null) + { + ws.Outline.SummaryVLocation = sheetProperty.OutlineProperties.SummaryBelow + ? XLOutlineSummaryVLocation.Bottom + : XLOutlineSummaryVLocation.Top; + } + + if (sheetProperty.OutlineProperties.SummaryRight != null) + { + ws.Outline.SummaryHLocation = sheetProperty.OutlineProperties.SummaryRight + ? XLOutlineSummaryHLocation.Right + : XLOutlineSummaryHLocation.Left; + } } - if (sheetProperty.OutlineProperties.SummaryRight != null) - { - ws.Outline.SummaryHLocation = sheetProperty.OutlineProperties.SummaryRight - ? XLOutlineSummaryHLocation.Right - : XLOutlineSummaryHLocation.Left; - } + if (sheetProperty.PageSetupProperties != null) + pageSetupProperties = sheetProperty.PageSetupProperties; } private static void LoadHeaderFooter(HeaderFooter headerFooter, XLWorksheet ws) @@ -1663,7 +1670,7 @@ ((XLHeaderFooter)ws.PageSetup.Footer).SetAsInitial(); } - private static void LoadPageSetup(PageSetup pageSetup, XLWorksheet ws) + private static void LoadPageSetup(PageSetup pageSetup, XLWorksheet ws, PageSetupProperties pageSetupProperties) { if (pageSetup == null) return; @@ -1671,11 +1678,16 @@ ws.PageSetup.PaperSize = (XLPaperSize)Int32.Parse(pageSetup.PaperSize.InnerText); if (pageSetup.Scale != null) ws.PageSetup.Scale = Int32.Parse(pageSetup.Scale.InnerText); - else + if (pageSetupProperties != null && pageSetupProperties.FitToPage != null && pageSetupProperties.FitToPage.Value) { - if (pageSetup.FitToWidth != null) + if (pageSetup.FitToWidth == null) + ws.PageSetup.PagesWide = 1; + else ws.PageSetup.PagesWide = Int32.Parse(pageSetup.FitToWidth.InnerText); - if (pageSetup.FitToHeight != null) + + if (pageSetup.FitToHeight == null) + ws.PageSetup.PagesTall = 1; + else ws.PageSetup.PagesTall = Int32.Parse(pageSetup.FitToHeight.InnerText); } if (pageSetup.PageOrder != null) diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index bc6df99..928b362 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -172,8 +172,8 @@ } } - // Get the CalculationChainPart - //Note: An instance of this part type contains an ordered set of references to all cells in all worksheets in the + // Get the CalculationChainPart + //Note: An instance of this part type contains an ordered set of references to all cells in all worksheets in the //workbook whose value is calculated from any formula CalculationChainPart calChainPart; @@ -207,7 +207,7 @@ var workbookPart = document.WorkbookPart ?? document.AddWorkbookPart(); var worksheets = WorksheetsInternal; - + var partsToRemove = workbookPart.Parts.Where(s => worksheets.Deleted.Contains(s.RelationshipId)).ToList(); @@ -555,7 +555,7 @@ { workbook.WorkbookProtection = null; } - + if (workbook.BookViews == null) workbook.BookViews = new BookViews(); @@ -4183,7 +4183,7 @@ } worksheetPart.Worksheet.InsertAfter(conditionalFormatting, previousElement); previousElement = conditionalFormatting; - cm.SetElement(XLWSContentManager.XLWSContents.ConditionalFormatting, conditionalFormatting); + cm.SetElement(XLWSContentManager.XLWSContents.ConditionalFormatting, conditionalFormatting); } } @@ -4373,15 +4373,11 @@ { pageSetup.Scale = null; - if (xlWorksheet.PageSetup.PagesWide > 0) + if (xlWorksheet.PageSetup.PagesWide >= 0 && xlWorksheet.PageSetup.PagesWide != 1) pageSetup.FitToWidth = (UInt32)xlWorksheet.PageSetup.PagesWide; - else - pageSetup.FitToWidth = 0; - if (xlWorksheet.PageSetup.PagesTall > 0) + if (xlWorksheet.PageSetup.PagesTall >= 0 && xlWorksheet.PageSetup.PagesTall != 1) pageSetup.FitToHeight = (UInt32)xlWorksheet.PageSetup.PagesTall; - else - pageSetup.FitToHeight = 0; } #endregion diff --git a/ClosedXML_Tests/Resource/Examples/PageSetup/TwoPages.xlsx b/ClosedXML_Tests/Resource/Examples/PageSetup/TwoPages.xlsx index 1e2deb7..76199d6 100644 --- a/ClosedXML_Tests/Resource/Examples/PageSetup/TwoPages.xlsx +++ b/ClosedXML_Tests/Resource/Examples/PageSetup/TwoPages.xlsx Binary files differ