diff --git a/ClosedXML/Excel/Style/XLFillKey.cs b/ClosedXML/Excel/Style/XLFillKey.cs index c2f2495..181b223 100644 --- a/ClosedXML/Excel/Style/XLFillKey.cs +++ b/ClosedXML/Excel/Style/XLFillKey.cs @@ -15,17 +15,33 @@ var hashCode = 2043579837; hashCode = hashCode * -1521134295 + BackgroundColor.GetHashCode(); hashCode = hashCode * -1521134295 + PatternColor.GetHashCode(); - hashCode = hashCode * -1521134295 + PatternType.GetHashCode(); + + var patternType = PatternType; + if (BackgroundColor.ColorType == XLColorType.Indexed && BackgroundColor.Indexed == 64) + patternType = XLFillPatternValues.None; + + hashCode = hashCode * -1521134295 + patternType.GetHashCode(); + return hashCode; } public bool Equals(XLFillKey other) { - return - (PatternType == XLFillPatternValues.None && other.PatternType == XLFillPatternValues.None) || - BackgroundColor == other.BackgroundColor - && PatternColor == other.PatternColor - && PatternType == other.PatternType; + if (PatternType == XLFillPatternValues.None && other.PatternType == XLFillPatternValues.None) + return true; + + var patternType1 = PatternType; + var patternType2 = other.PatternType; + + if (BackgroundColor.ColorType == XLColorType.Indexed && BackgroundColor.Indexed == 64) + patternType1 = XLFillPatternValues.None; + + if (other.BackgroundColor.ColorType == XLColorType.Indexed && other.BackgroundColor.Indexed == 64) + patternType2 = XLFillPatternValues.None; + + return BackgroundColor == other.BackgroundColor + && PatternColor == other.PatternColor + && patternType1 == patternType2; } public override bool Equals(object obj) diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 52e2211..74edc65 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -3982,7 +3982,8 @@ break; } - patternFill.AppendChild(backgroundColor); + if (backgroundColor.HasChildren) + patternFill.AppendChild(backgroundColor); } else { @@ -3995,7 +3996,11 @@ break; case XLColorType.Indexed: - foregroundColor.Indexed = (UInt32)fillInfo.Fill.BackgroundColor.Indexed; + // 64 is 'transparent' and should be ignored for differential formats + if (fillInfo.Fill.BackgroundColor.Indexed != 64) + foregroundColor.Indexed = (UInt32)fillInfo.Fill.BackgroundColor.Indexed; + + //foregroundColor.Indexed = (UInt32)fillInfo.Fill.BackgroundColor.Indexed; break; case XLColorType.Theme: @@ -4007,7 +4012,8 @@ break; } - patternFill.AppendChild(foregroundColor); + if (foregroundColor.HasChildren) + patternFill.AppendChild(foregroundColor); } break; @@ -4033,7 +4039,8 @@ break; } - patternFill.AppendChild(foregroundColor); + if (foregroundColor.HasChildren) + patternFill.AppendChild(foregroundColor); backgroundColor = new BackgroundColor(); switch (fillInfo.Fill.BackgroundColor.ColorType) @@ -4055,14 +4062,19 @@ break; } - patternFill.AppendChild(backgroundColor); + if (backgroundColor.HasChildren) + patternFill.AppendChild(backgroundColor); break; } - fill.AppendChild(patternFill); + if (patternFill.HasChildren) + fill.AppendChild(patternFill); - return fill; + if (fill.HasChildren) + return fill; + else + return null; } private bool FillsAreEqual(Fill f, XLFillValue xlFill, Boolean fromDifferentialFormat)