Inconsistent appearance between manual and coded versions of solid databar and databar minimum value

后端 未结 1 510
攒了一身酷
攒了一身酷 2021-01-13 19:12

I am trying to create solid databars in EPPlus 4.0.4, and am running into two problems.

  • First, I haven\'t been able to figure out how to create a solid fill co
1条回答
  •  臣服心动
    2021-01-13 19:35

    This is an extension list problem. This comes up alot when getting into more complex exports. Conditional formatting is probably one of the tougher ones because there are so many nuances and it has changed so much over the years.

    Extension list (extLst tags in xml) is kind of a catchall bucket that the OpenOfficeXml standard can use to added new features and formatting. In your case Excel populates the extension list section to allow for the extended min/max limit. Epplus does not support this which is why you see the difference.

    Your simplest option would be just to inject it yourself via xml/string manipulation Not pretty but it gets the job done:

    var bars = doc.ConditionalFormatting.AddDatabar(range, Color.FromArgb(99, 195, 132));
    
    bars.HighValue.Type = eExcelConditionalFormattingValueObjectType.Num;
    bars.LowValue.Type = eExcelConditionalFormattingValueObjectType.Num;
    
    bars.HighValue.Value = numResponses; //82
    bars.LowValue.Value = 0;
    
    //Get reference to the worksheet xml for proper namespace
    var xdoc = doc.WorksheetXml;
    var nsm = new XmlNamespaceManager(xdoc.NameTable);
    nsm.AddNamespace("default", xdoc.DocumentElement.NamespaceURI);
    
    //Create the conditional format extension list entry
    var extLstCf = xdoc.CreateNode(XmlNodeType.Element, "extLst", xdoc.DocumentElement.NamespaceURI);
    extLstCf.InnerXml = @"{3F3F0E19-800E-4C9F-9CAF-1E3CE014ED86}";
    var cfNode = xdoc.SelectSingleNode("/default:worksheet/default:conditionalFormatting/default:cfRule", nsm);
    cfNode.AppendChild(extLstCf);
    
    //Create the extension list content for the worksheet
    var extLstWs = xdoc.CreateNode(XmlNodeType.Element, "extLst", xdoc.DocumentElement.NamespaceURI);
    extLstWs.InnerXml = @"082B2:B11";
    var wsNode = xdoc.SelectSingleNode("/default:worksheet", nsm);
    wsNode.AppendChild(extLstWs);
    
    pck.Save();
    

    Note the gradient=""0"" which will set the color bars to solid instead of a gradient as well as the min/max settings to get the spread you are looking for.

    A more "proper" way would be to would to recreate the xml objects node by node and attribute by attribute which will take a while but only have to do it once.

    0 讨论(0)
提交回复
热议问题