Extend EPPlus’s Functionality With XPathPosted on Feb 14, 2013 by Nate in Uncategorized
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 Open Office Xml format, which means you can go beyond its code base and extend it with your very own functions. When EPPlus kicks out your .xslx 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 assign the color palette my client needed:
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.