diff --git a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs index 703d006..3b172f8 100644 --- a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs +++ b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs @@ -1,5 +1,8 @@ using ClosedXML.Extensions; +using DocumentFormat.OpenXml.Office.Excel; using DocumentFormat.OpenXml.Office2010.Excel; +using System; +using System.Linq; namespace ClosedXML.Excel { @@ -22,11 +25,25 @@ MinLength = 0, MaxLength = 100, Gradient = false, - AxisPosition = DataBarAxisPositionValues.Middle + AxisPosition = DataBarAxisPositionValues.Middle, + ShowValue = !cf.ShowBarOnly }; - var cfMin = new ConditionalFormattingValueObject { Type = ConditionalFormattingValueObjectTypeValues.AutoMin }; - var cfMax = new ConditionalFormattingValueObject() { Type = ConditionalFormattingValueObjectTypeValues.AutoMax }; + ConditionalFormattingValueObjectTypeValues cfMinType = Convert(cf.ContentTypes[1].ToOpenXml()); + var cfMin = new ConditionalFormattingValueObject { Type = cfMinType }; + if (cf.Values.Any() && cf.Values[1]?.Value != null) + { + cfMin.Type = ConditionalFormattingValueObjectTypeValues.Numeric; + cfMin.Append(new Formula() { Text = cf.Values[1].Value }); + } + + ConditionalFormattingValueObjectTypeValues cfMaxType = Convert(cf.ContentTypes[2].ToOpenXml()); + var cfMax = new ConditionalFormattingValueObject { Type = cfMaxType }; + if (cf.Values.Count >= 2 && cf.Values[2]?.Value != null) + { + cfMax.Type = ConditionalFormattingValueObjectTypeValues.Numeric; + cfMax.Append(new Formula() { Text = cf.Values[2].Value }); + } var barAxisColor = new BarAxisColor { Rgb = XLColor.Black.Color.ToHex() }; @@ -46,5 +63,26 @@ return conditionalFormattingRule; } + + private ConditionalFormattingValueObjectTypeValues Convert(DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues obj) + { + switch (obj) + { + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Max: + return ConditionalFormattingValueObjectTypeValues.AutoMax; + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Min: + return ConditionalFormattingValueObjectTypeValues.AutoMin; + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Number: + return ConditionalFormattingValueObjectTypeValues.Numeric; + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Percent: + return ConditionalFormattingValueObjectTypeValues.Percent; + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Percentile: + return ConditionalFormattingValueObjectTypeValues.Percentile; + case DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValueObjectValues.Formula: + return ConditionalFormattingValueObjectTypeValues.Formula; + default: + throw new NotImplementedException(); + } + } } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 9979eec..9284747 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -4517,6 +4517,17 @@ WorksheetExtensionList worksheetExtensionList = worksheetPart.Worksheet.Elements().First(); cm.SetElement(XLWSContentManager.XLWSContents.WorksheetExtensionList, worksheetExtensionList); + var conditionalFormattings = worksheetExtensionList.Descendants().SingleOrDefault(); + if (conditionalFormattings == null || !conditionalFormattings.Any()) + { + WorksheetExtension worksheetExtension1 = new WorksheetExtension { Uri = "{78C0D931-6437-407d-A8EE-F0AAD7539E65}" }; + worksheetExtension1.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"); + worksheetExtensionList.Append(worksheetExtension1); + + conditionalFormattings = new DocumentFormat.OpenXml.Office2010.Excel.ConditionalFormattings(); + worksheetExtension1.Append(conditionalFormattings); + } + foreach (var cfGroup in exlst .GroupBy( c => c.Range.RangeAddress.ToStringRelative(false), @@ -4527,16 +4538,13 @@ { foreach (var xlConditionalFormat in cfGroup.CfList.Cast()) { - var conditionalFormattingRule = worksheetExtensionList.Descendants() + var conditionalFormattingRule = conditionalFormattings.Descendants() .SingleOrDefault(r => r.Id == xlConditionalFormat.Id.WrapInBraces()); if (conditionalFormattingRule != null) { - WorksheetExtension worksheetExtension = conditionalFormattingRule.Ancestors().SingleOrDefault(); - worksheetExtensionList.RemoveChild(worksheetExtension); + var conditionalFormat = conditionalFormattingRule.Ancestors().SingleOrDefault(); + conditionalFormattings.RemoveChild(conditionalFormat); } - WorksheetExtension worksheetExtension1 = new WorksheetExtension { Uri = "{78C0D931-6437-407d-A8EE-F0AAD7539E65}" }; - worksheetExtension1.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"); - var conditionalFormattings = new DocumentFormat.OpenXml.Office2010.Excel.ConditionalFormattings(); var conditionalFormatting = new DocumentFormat.OpenXml.Office2010.Excel.ConditionalFormatting(); conditionalFormatting.AddNamespaceDeclaration("xm", "http://schemas.microsoft.com/office/excel/2006/main"); @@ -4545,9 +4553,6 @@ conditionalFormatting.Append(referenceSequence); conditionalFormattings.Append(conditionalFormatting); - worksheetExtension1.Append(conditionalFormattings); - - worksheetExtensionList.Append(worksheetExtension1); } } } diff --git a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs index 70d8f24..27656bf 100644 --- a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs +++ b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs @@ -531,16 +531,27 @@ var workbook = new XLWorkbook(); var ws = workbook.AddWorksheet("Sheet1"); - ws.FirstCell().SetValue(-1) + ws.Cell(1, 1).SetValue(-1) .CellBelow().SetValue(1) .CellBelow().SetValue(2) .CellBelow().SetValue(3); - ws.RangeUsed() + ws.Range(ws.Cell(1, 1), ws.Cell(4, 1)) .AddConditionalFormat() .DataBar(XLColor.Green, XLColor.Red, showBarOnly: false) .LowestValue() - .Maximum(XLCFContentType.Percent, "100"); + .HighestValue(); + + ws.Cell(1,3).SetValue(-20) + .CellBelow().SetValue(40) + .CellBelow().SetValue(-60) + .CellBelow().SetValue(30); + + ws.Range(ws.Cell(1, 3), ws.Cell(4, 3)) + .AddConditionalFormat() + .DataBar(XLColor.Green, XLColor.Red, showBarOnly: true) + .Minimum(XLCFContentType.Number, -100) + .Maximum(XLCFContentType.Number, 100); workbook.SaveAs(filePath); } @@ -652,4 +663,4 @@ workbook.SaveAs(filePath); } } -} \ No newline at end of file +} diff --git a/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFDataBarNegative.xlsx b/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFDataBarNegative.xlsx index a3f79f6..fa46948 100644 --- a/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFDataBarNegative.xlsx +++ b/ClosedXML_Tests/Resource/Examples/ConditionalFormatting/CFDataBarNegative.xlsx Binary files differ