From the archive: written in 2013. Some tools and links may have moved on since.

While EPPlus has many built-in functions that allow you to optimize your Excel charts, your client might want a feature that the library just doesn’t support. However, there’s a way around this.

EPPlus uses the Office Open XML format, which means you can go beyond its code base and extend it with your very own functions. When EPPlus kicks out your .xlsx file, open it and make whatever change your client needs within Excel. Save your changes, close the file, and change the file extension to .zip. Once you do that, you’ll be able to go into the different xml files that make up your spreadsheet. With a little work, you can find the xml nodes that were changed. Examine their location and markup within the xml structure.

Then, in your code base, you can create a method like the one below, which uses XPath to set the color my client needed for negative values in a bar chart:

private static void setBarChartNegativeValColor(ExcelChart chart) {
    XmlNamespaceManager xnsm = new XmlNamespaceManager(chart.ChartXml.NameTable);
    xnsm.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
    XmlNode barChart = chart.ChartXml.SelectSingleNode("//c:barChart/c:ser", xnsm);
    XmlDocument colorDoc = new XmlDocument();

    string xml = "<c:extLst xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\"
        xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"
        xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">
        <c:ext uri=\"{6F2FDCE9-48DA-4B69-8628-5D25D57E5C99}\"
        xmlns:c14=\"http://schemas.microsoft.com/office/drawing/2007/8/2/chart\"><c14:invertSolidFillFmt>
        <c14:spPr xmlns:c14=\"http://schemas.microsoft.com/office/drawing/2007/8/2/chart\"><a:solidFill>
        <a:srgbClr val=\"fc2c16\" /></a:solidFill></c14:spPr></c14:invertSolidFillFmt></c:ext></c:extLst>";

    colorDoc.LoadXml(xml);

    XmlNode colorNode = chart.ChartXml.ImportNode(colorDoc.DocumentElement, true);

    barChart.AppendChild(colorNode);
}

All I really had to do was copy the xml that was generated by the changes I needed, and programmatically insert it into the appropriate spot within the document. With this approach, you can set any option your client needs, even if EPPlus doesn’t directly provide the functionality.

All writing