diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 9e92af5..ae5af7c 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -3405,7 +3405,7 @@ foreach (CellFormat f in workbookStylesPart.Stylesheet.CellFormats) { styleId++; - if (CellFormatsAreEqual(f, ss.Value)) + if (CellFormatsAreEqual(f, ss.Value, compareAlignment: true)) break; } if (styleId == -1) @@ -3572,7 +3572,7 @@ { var info = styleInfo; var foundOne = - workbookStylesPart.Stylesheet.CellFormats.Cast().Any(f => CellFormatsAreEqual(f, info)); + workbookStylesPart.Stylesheet.CellFormats.Cast().Any(f => CellFormatsAreEqual(f, info, compareAlignment: true)); if (foundOne) continue; @@ -3611,7 +3611,7 @@ var info = styleInfo; var foundOne = workbookStylesPart.Stylesheet.CellStyleFormats.Cast().Any( - f => CellFormatsAreEqual(f, info)); + f => CellFormatsAreEqual(f, info, compareAlignment: false)); if (foundOne) continue; @@ -3672,7 +3672,17 @@ }; } - private static bool CellFormatsAreEqual(CellFormat f, StyleInfo styleInfo) + /// + /// Check if two style are equivalent. + /// + /// Style in the OpenXML format. + /// Style in the ClosedXML format. + /// Flag specifying whether or not compare the alignments of two styles. + /// Styles in x:cellStyleXfs section do not include alignment so we don't have to compare it in this case. + /// Styles in x:cellXfs section, on the opposite, do include alignments, and we must compare them. + /// + /// True if two formats are equivalent, false otherwise. + private static bool CellFormatsAreEqual(CellFormat f, StyleInfo styleInfo, bool compareAlignment) { return f.BorderId != null && styleInfo.BorderId == f.BorderId @@ -3683,7 +3693,7 @@ f.ApplyFill != null && f.ApplyFill == ApplyFill(styleInfo)) && (f.ApplyBorder == null && styleInfo.Style.Border == XLBorderValue.Default || f.ApplyBorder != null && f.ApplyBorder == ApplyBorder(styleInfo)) - && (f.Alignment == null || AlignmentsAreEqual(f.Alignment, styleInfo.Style.Alignment)) + && (!compareAlignment || AlignmentsAreEqual(f.Alignment, styleInfo.Style.Alignment)) && ProtectionsAreEqual(f.Protection, styleInfo.Style.Protection) ; } diff --git a/ClosedXML_Tests/Excel/Saving/SavingTests.cs b/ClosedXML_Tests/Excel/Saving/SavingTests.cs index bd7b27e..1cbf38d 100644 --- a/ClosedXML_Tests/Excel/Saving/SavingTests.cs +++ b/ClosedXML_Tests/Excel/Saving/SavingTests.cs @@ -490,5 +490,23 @@ } } } + + [Test] + public void PreserveAlignmentOnSaving() + { + using (var input = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\HorizontalAlignment.xlsx"))) + using (var output = new MemoryStream()) + { + using (var wb = new XLWorkbook(input)) + { + wb.SaveAs(output); + } + + using (var wb = new XLWorkbook(output)) + { + Assert.AreEqual(XLAlignmentHorizontalValues.Center, wb.Worksheets.First().Cell("B1").Style.Alignment.Horizontal); + } + } + } } } diff --git a/ClosedXML_Tests/Resource/Misc/HorizontalAlignment.xlsx b/ClosedXML_Tests/Resource/Misc/HorizontalAlignment.xlsx new file mode 100644 index 0000000..319da43 --- /dev/null +++ b/ClosedXML_Tests/Resource/Misc/HorizontalAlignment.xlsx Binary files differ diff --git a/ClosedXML_Tests/Resource/Other/StyleReferenceFiles/TransparentBackgroundFill/TransparentBackgroundFill.xlsx b/ClosedXML_Tests/Resource/Other/StyleReferenceFiles/TransparentBackgroundFill/TransparentBackgroundFill.xlsx index 8d63ec0..be28d7b 100644 --- a/ClosedXML_Tests/Resource/Other/StyleReferenceFiles/TransparentBackgroundFill/TransparentBackgroundFill.xlsx +++ b/ClosedXML_Tests/Resource/Other/StyleReferenceFiles/TransparentBackgroundFill/TransparentBackgroundFill.xlsx Binary files differ