diff --git a/ClosedXML/Excel/Style/XLFill.cs b/ClosedXML/Excel/Style/XLFill.cs index d5d6849..3dc4fdc 100644 --- a/ClosedXML/Excel/Style/XLFill.cs +++ b/ClosedXML/Excel/Style/XLFill.cs @@ -125,7 +125,20 @@ get { return Key.PatternType; } set { - Modify(k => { k.PatternType = value; return k; }); + if (PatternType == XLFillPatternValues.None && + value != XLFillPatternValues.None) + { + // If fill was empty and the pattern changes to non-empty we have to specify a background color too. + // Otherwise the fill will be considered empty and pattern won't update (the cached empty fill will be used). + Modify(k => + { + k.BackgroundColor = XLColor.FromTheme(XLThemeColor.Text1).Key; + k.PatternType = value; + return k; + }); + } + else + Modify(k => { k.PatternType = value; return k; }); } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 74edc65..57e3f00 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -3982,7 +3982,7 @@ break; } - if (backgroundColor.HasChildren) + if (backgroundColor.HasAttributes) patternFill.AppendChild(backgroundColor); } else @@ -4012,7 +4012,7 @@ break; } - if (foregroundColor.HasChildren) + if (foregroundColor.HasAttributes) patternFill.AppendChild(foregroundColor); } break; @@ -4039,7 +4039,7 @@ break; } - if (foregroundColor.HasChildren) + if (foregroundColor.HasAttributes) patternFill.AppendChild(foregroundColor); backgroundColor = new BackgroundColor(); @@ -4062,7 +4062,7 @@ break; } - if (backgroundColor.HasChildren) + if (backgroundColor.HasAttributes) patternFill.AppendChild(backgroundColor); break; @@ -4071,10 +4071,7 @@ if (patternFill.HasChildren) fill.AppendChild(patternFill); - if (fill.HasChildren) - return fill; - else - return null; + return fill; } private bool FillsAreEqual(Fill f, XLFillValue xlFill, Boolean fromDifferentialFormat)