Adding Custom Colors for Cells in Excel Export
Adding Custom Colors for Cells in Excel Export
I had a question regarding adding custom colors for excel cells in exports. There are a few threads which talk about this but I haven't found any good examples of doing this. From what I have read from thread such as this https://datatables.net/forums/discussion/40551/how-can-i-change-the-background-color-for-a-cell-excel-export are that we need to modify the XML file to include your custom styling. One way is to manually go into it and hardcode it. But the other way I've read and the way I want to do it is to add to it during the export through the customize function. I've read about this several times in threads but haven't saw any actual examples about it. I was wondering if anybody had one they can show. Thank you for any help.
This question has an accepted answers - jump to answer
Answers
I add custom cell colors in here. It is basically a change history view on my log. Whenever a cell value changes within the same contract it is colored. To make it easier to track log changes.
This is what is looks like. Column B shows whether a record is "new" or just "edited". Everything is ordered by column D and C ascending.
And this is the customize method. You'll figure it out. Good luck!
Here is one of the global variables I defined that makes life easier. Of course you can calculate the Excel column letter. But I don't like math and this is so much easier
This is the relevant part of the docs:
https://datatables.net/reference/button/excelHtml5
Search for "Built in Styles". Of course you can define your own styles and you'll find posts on that in this forum. But that isn't trivial.
Thanks for your example. I was hoping to use my own colors instead of the built in styles but as you say its not that trivial. But I'm a bit curious, would you happen to know how to proceed with doing that though?
ok, here is another "customize" method from a different button. I create my own styles here. I don't have your use case (proprietary fills) but that should work the same way. You would need to fiddle with this:
//styleSheet.childNodes[0].childNodes[2] ==> fills <fills count="6"> </fills>
This code in https://cdn.datatables.net/buttons/2.3.5/js/buttons.html5.js
would need to be appended by you programmatically at run time to make it work. I don't recommend "hacking" the file itself because then you would need to redo this at every data table version change. One key insight is: You cannot apply multiple styles to one cell. If you need a combination of several things e.g. bold, underlined, purple background, you would need to create it as one style combining all of those features. That makes it cumbersome. I don't do this any more. So this "customize" method of mine is very old.
This was exactly what I was looking for. Thank you so much for the help.