Excel export with background color without affecting the formatting
Excel export with background color without affecting the formatting
Hello I have been struggling for hours searching over and over on how to export with conditional formatting, I've been looking a lot in the forums and apologize if its a dupplicate
My most efficient way is to use a class because we have a php loop that create the table withsome data stored in SQL with targets and with many different formats ( time, percent, integers etc..) so i can apply easily a class in the backend but i have 2 issues.
1 i want to keep the number format that was there i only want to change the background color
2 If someone alter the order, the indexing is being all screwed and excel format is being applied on the wrong cell.
var StatTable= $('#StatTable').DataTable({
dom: 'Blfrtip',
buttons: [ {
extend: 'excelHtml5',
title: 'Performance_Tracker',
customize: function(xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
var notmetlist = StatTable.cells('.NotMet').indexes().toArray();
notmetlist.forEach(function(element){
var c = element['column']+1;
var r = element['row']+3;
$('row:nth-child(' + r + ') c:nth-child('+ c +')', sheet).attr( 's', '12' );
});}}]});
is there a way to do this? I need to have a excel background color of red if a datatable cell has a class of not met.
thanks!
This question has accepted answers - jump to:
Answers
Yup - Excel is a huge pain with this. In Open Spreadsheet a style is a combination of the background colour, border, font and formatting. Its not like CSS where you can just add another class to get appropriate styling.
So what you have to do is create a new style which is the combination that you want and then add use that new style.
This is where the built in styles are for Buttons. You could use that as a guide for what you want to do. You'll need to add a new one to that list in the
customize
array.Its a pain yes. Buttons is not designed to be an abstraction layer for Excel though I'm afraid. You'd need to use another library such as SheetJS if you want complete control over the Excel output.
Allan
Hi Thank you for this complete answer, i think ill have to dig deeper into other plugins or adding this customized style, but ill need 1 for each type, i think i could manage this
1 more question, can we get the "line number" and not the row index of everyrowwith a class or reindex those rows from the current order?
i need to replace this line? ( or add before!)
because the reordering kills me and i can't remove this function to my users.
thanks!
I'm afraid I don't understand. Could you illustrate with an example?
Allan
Hi Francois,
I guess you have trouble finding your rows if the users keep reordering the rows and you don't know their position afterwards?!
If you have some kind of "line number" or some other attribute in the data you export that you can use to identify the right row then it shouldn't be too difficult.
This code is from my customize: function(xlsx)
As you can see I identify rows by their content in column B (e.g. "Summen per Laufzeitende" or "Totals"). You could do the same with your "line number".
In case you need a full example of a customize function including the creation of your own styles just let me know I can post the example here if you like.
Hi, this is a great idea to show a line number based on the index, doing so will give me an option to track it once transferred to Excel.
And yes, rf1234 if you can give me a clean example on how to make it red with a "percentage" value, it would be really appreciated! i've been unable to make it happen with my tries i don't know why
So Allan to explain a bit more my situation is that when i export to excel, the lines are being created by the current view order and not the index number (it is good because the user , so my row 3 in excel is not necessary my index 0, I thought i could use index + 3 but if the user modified the order it doesnt works and the format is being applied to the wrong cell. If i could force datatables to recreate the index number based on the visible row that would fix it all. so no mater how many times my columns were reordered, the first row would have an index of 0. second = 1 based on the visible data. OR if I could get the visible line number instead of the index it would help also.
thanks to both of you!
You can control the export order using the
order
property of the export options (see theselector-modifier
docs). You can select to have the export in the currently applied order, or the original data order.Allan
ahh cool that is also a good idea. i think i have things to work with. many thanks!
Hi François,
so this is my Excel export button definition including some styles:
I do a lot of reporting too. But I use PHP Spreadsheet for this; much easier.
Roland