diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index af825e4..0ad3737 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -4043,7 +4043,7 @@ worksheetPart.Worksheet.RemoveAllChildren(); var previousElement = cm.GetPreviousElementFor(XLWSContentManager.XLWSContents.ConditionalFormatting); - var priority = 0; + var priority = 1; // priority is 1 origin in Microsoft Excel foreach (var cfGroup in xlWorksheet.ConditionalFormats .GroupBy( c => c.Range.RangeAddress.ToStringRelative(false), @@ -4052,8 +4052,6 @@ ) ) { - - priority++; var conditionalFormatting = new ConditionalFormatting { SequenceOfReferences = @@ -4062,6 +4060,7 @@ foreach(var cf in cfGroup.CfList) { conditionalFormatting.Append(XLCFConverters.Convert(cf, priority, context)); + priority++; } worksheetPart.Worksheet.InsertAfter(conditionalFormatting, previousElement); previousElement = conditionalFormatting; diff --git a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs index f87825b..efd53e8 100644 --- a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs +++ b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs @@ -593,4 +593,25 @@ workbook.SaveAs(filePath); } } + + public class CFMultipleConditions : IXLExample + { + public void Create(String filePath) + { + var workbook = new XLWorkbook(); + var ws = workbook.AddWorksheet("Sheet1"); + + using(var range = ws.Range("A1:A10")) + { + range.AddConditionalFormat().WhenEquals("3") + .Fill.SetBackgroundColor(XLColor.Blue); + range.AddConditionalFormat().WhenEquals("2") + .Fill.SetBackgroundColor(XLColor.Green); + range.AddConditionalFormat().WhenEquals("1") + .Fill.SetBackgroundColor(XLColor.Red); + } + + workbook.SaveAs(filePath); + } + } } diff --git a/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML_Tests/ClosedXML_Tests.csproj index d6f595c..80cf986 100644 --- a/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -230,6 +230,7 @@ + @@ -240,8 +241,7 @@ - - + diff --git a/ClosedXML_Tests/Examples/ConditionalFormattingTests.cs b/ClosedXML_Tests/Examples/ConditionalFormattingTests.cs index f5e9bcd..823ebeb 100644 --- a/ClosedXML_Tests/Examples/ConditionalFormattingTests.cs +++ b/ClosedXML_Tests/Examples/ConditionalFormattingTests.cs @@ -102,13 +102,12 @@ TestHelper.RunTestExample(@"ConditionalFormatting\CFStartsWith.xlsx"); } - // - //[Test] - //public void XXX() - //{ - // TestHelper.RunTestExample(@"ConditionalFormatting\XXX.xlsx"); - //} - // + [Test] + public void CFMultipleConditions() + { + TestHelper.RunTestExample(@"ConditionalFormatting\CFMultipleConditions.xlsx"); + } + //[Test] //public void XXX() //{ diff --git a/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFMultipleConditions.xlsx b/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFMultipleConditions.xlsx new file mode 100644 index 0000000..4a5e54a --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFMultipleConditions.xlsx Binary files differ