diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs index 98bccb7..c89ebba 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs @@ -57,8 +57,8 @@ public bool StyleChanged { get; set; } public IXLRange Range { get; set; } public XLConditionalFormatType ConditionalFormatType { get; set; } - public XLTimePeriod TimePeriod { get; private set; } - public XLIconSetStyle IconSetStyle { get; private set; } + public XLTimePeriod TimePeriod { get; set; } + public XLIconSetStyle IconSetStyle { get; set; } public XLDictionary Values { get; private set; } public XLDictionary Colors { get; private set; } public XLDictionary ContentTypes { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index 446b8c4..fc06bce 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -23,6 +23,7 @@ using Ap; using Op; using System.Xml.Linq; + using System.Text.RegularExpressions; #endregion @@ -1294,30 +1295,45 @@ conditionalFormat.ConditionalFormatType = fr.Type.Value.ToClosedXml(); if (fr.Text != null) conditionalFormat.Values.Add(fr.Text.Value); - + if (fr.Percent != null) + conditionalFormat.Percent = fr.Percent.Value; + if (fr.Bottom != null) + conditionalFormat.Bottom = fr.Bottom.Value; + if(fr.Rank != null) + conditionalFormat.Values.Add(fr.Rank.Value.ToString()); if (fr.Elements().Any()) { var colorScale = fr.Elements().First(); - foreach (var c in colorScale.Elements()) - { - conditionalFormat.ContentTypes.Add(c.Type.Value.ToClosedXml() ); - conditionalFormat.Values.Add(c.Val.Value); - } - foreach (var c in colorScale.Elements()) - { - conditionalFormat.Colors.Add(GetColor(c)); - } + ExtractConditionalFormatValueObjects(conditionalFormat, colorScale); + } + else if (fr.Elements().Any()) + { + var dataBar = fr.Elements().First(); + if (dataBar.ShowValue != null) + conditionalFormat.ShowBarOnly = !dataBar.ShowValue.Value; + ExtractConditionalFormatValueObjects(conditionalFormat, dataBar); + } + else if (fr.Elements().Any()) + { + var iconSet = fr.Elements().First(); + if (iconSet.ShowValue != null) + conditionalFormat.ShowIconOnly = !iconSet.ShowValue.Value; + if (iconSet.Reverse != null) + conditionalFormat.ReverseIconOrder = iconSet.Reverse.Value; + if (iconSet.IconSetValue != null) + conditionalFormat.IconSetStyle = iconSet.IconSetValue.Value.ToClosedXml(); + ExtractConditionalFormatValueObjects(conditionalFormat, iconSet); } else { foreach (var formula in fr.Elements()) { - if (formula.Text != null) + if (formula.Text != null && conditionalFormat.ConditionalFormatType == XLConditionalFormatType.CellIs) { String val = formula.Text; - if (val.StartsWith("\"")) val = val.Substring(1, val.Length - 2); - conditionalFormat.Values.Add(val); + if (val.StartsWith("\"")) val = val.Substring(1, val.Length - 2); + conditionalFormat.Values.Add(val); } } } @@ -1329,6 +1345,23 @@ } + private void ExtractConditionalFormatValueObjects(XLConditionalFormat conditionalFormat, OpenXmlElement element) + { + foreach (var c in element.Elements()) + { + if (c.Type != null) + conditionalFormat.ContentTypes.Add(c.Type.Value.ToClosedXml()); + if (c.Val != null) + conditionalFormat.Values.Add(c.Val.Value); + if(c.GreaterThanOrEqual != null) + conditionalFormat.IconSetOperators.Add(c.GreaterThanOrEqual.Value ? XLCFIconSetOperator.EqualOrGreaterThan : XLCFIconSetOperator.GreaterThan); + } + foreach (var c in element.Elements()) + { + conditionalFormat.Colors.Add(GetColor(c)); + } + } + private static void LoadHyperlinks(Hyperlinks hyperlinks, WorksheetPart worksheetPart, XLWorksheet ws) { var hyperlinkDictionary = new Dictionary(); diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs index b001f50..b50c6e3 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs @@ -526,12 +526,11 @@ .CellBelow().SetValue(2) .CellBelow().SetValue(3); - ws.RangeUsed().AddConditionalFormat().ColorScale() - .LowestValue(XLColor.Red) - .Midpoint(XLCFContentType.Percent, "50", XLColor.Yellow) - .HighestValue(XLColor.Green); + ws.RangeUsed().AddConditionalFormat().IconSet(XLIconSetStyle.ThreeTrafficLights2, true, true) + .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "0", XLCFContentType.Number) + .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "2", XLCFContentType.Number) + .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "3", XLCFContentType.Number); - workbook.SaveAs(filePath); } }