Excel export: how to align text to the center in excel cell?
Excel export: how to align text to the center in excel cell?
elen11
Posts: 6Questions: 4Answers: 0
Hello,
could you tell me, please:
How to set alignment center to the excel cells while exporting?
I tried this, but it doesn't work:
var col = $('col', sheet);
col.each(function () {
$(this).attr('alignment horizontal', 'center');
});
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You will need to use the
customize
function of the excel export button to set the style of the the desired cells. Here is an example:https://datatables.net/extensions/buttons/examples/html5/excelTextBold.html
This page show the Datatables built-in styles:
https://datatables.net/reference/button/excelHtml5
Looks like style
51
is what you need.Kevin
Kevin thank you for answer.
This code was within customize function (it is just too big, so I didn't post it here)
But if I apply style 51 to these cells, will I be able to apply another styles to same cells too?
After I will need to apply currency and percentage style, and when I try to do something like this:
$('row c[r^="G5"]', sheet).attr('s', ['51', '56']);
it is just doesn't work.
So probably I need to add some attribute to all rows first to align text?
I think you will need to create custom styles, combining what you need, to add to the built-in styles. This thread has an example:
https://datatables.net/forums/discussion/43973/excel-customization-with-4-decimal-places#latest
Kevin