I had the same problem, so here I write what worked for me after search a lot. If someone is looking another way to reach this in the future.
Excel needs US number format in order to work (at least is what I read about the Excel generated from datatable), so before create the excel, the datatable needs to format it the proper way. Then the excel itself (in my case Libre office) shows that number in the format of the excel instalation. If is an US instalation it should show the US format, if it's Hungarian it should show Hungarian, if it's Spanish, Spanish, and so on.
First you create a formatter. Inside the function that is in "body" you format your data. Here I looked for the column 7 that was the one that have the numbers that were exported in an incorrect format.
I show in the datatable the format for example 1.000.676,50, so on the function, first I deleted the point, so numero now should be 1000676,50. Then I change the comma by a point, and the number now is 1000676.50 . With that number now excel can work with that cell.
let exportFormatter = {
format: {
body: function (data, row, column, node) {
var numero = '';
if(column === 7){
numero = data.replace(/\./g, '');
numero = numero.replace(/,/g , '.');
return numero;
}
return data;
}
}
};
Then you add that formatter, is the line that says exportOptions.
I also used the style s66 for the problematic cell ($('row c[r^="H'+ num +'"]', sheet).attr('s', '66');), you can use the one that serves you most.
Here is the datatables page that shows the list of them:
https://datatables.net/reference/button/excelHtml5
"language": {
"url": "js/dtSpanish.json"
},
"pagingType": "full_numbers",
"mData": null,
"iDisplayLength": 25,
"aaSorting": [],
"columnDefs": [
{"orderable": false}
],
dom: 'Blfrtip',
buttons: [
{
extend: 'pdfHtml5',
title: 'Consulta recargo clientes',
orientation: 'landscape',
pageSize: 'LEGAL',
customize: function (doc) {
var rowCount = doc.content[1].table.body.length;
for (i = 1; i < rowCount; i++) {
doc.content[1].table.body[i][0].alignment = 'center';
doc.content[1].table.body[i][1].alignment = 'center';
doc.content[1].table.body[i][2].alignment = 'center';
doc.content[1].table.body[i][3].alignment = 'center';
doc.content[1].table.body[i][4].alignment = 'center';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
doc.content[1].table.body[i][8].alignment = 'center';
}
}
},
{
extend: 'excelHtml5',
title: 'Consulta recargo clientes',
orientation: 'landscape',
exportOptions: exportFormatter,
customize:function (xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row c[r^="A2"]', sheet).attr('s', '32');
$('row c[r^="B2"]', sheet).attr('s', '32');
$('row c[r^="C2"]', sheet).attr('s', '32');
$('row c[r^="D2"]', sheet).attr('s', '32');
$('row c[r^="E2"]', sheet).attr('s', '32');
$('row c[r^="F2"]', sheet).attr('s', '32');
$('row c[r^="G2"]', sheet).attr('s', '32');
$('row c[r^="H2"]', sheet).attr('s', '32');
$('row c[r^="I2"]', sheet).attr('s', '32');
$('row c[r^="J2"]', sheet).attr('s', '32');
var table = $('#tablaContratosFacturados').DataTable();
var numero = table.rows().count();
for(i = 1 ; i <= numero; i++){
var num = i + 2;
$('row c[r^="A'+ num +'"]', sheet).attr('s', '51');
$('row c[r^="B'+ num +'"]', sheet).attr('s', '51');
$('row c[r^="C'+ num +'"]', sheet).attr('s', '51');
$('row c[r^="D'+ num +'"]', sheet).attr('s', '51');
$('row c[r^="E'+ num +'"]', sheet).attr('s', '51');
$('row c[r^="F'+ num +'"]', sheet).attr('s', '52');
$('row c[r^="G'+ num +'"]', sheet).attr('s', '52');
$('row c[r^="H'+ num +'"]', sheet).attr('s', '66');
$('row c[r^="I'+ num +'"]', sheet).attr('s', '51');
}
}
}
]
});
I'm document it here before i forget about it. Hope someone can find it useful in a future and can adapt it to their needs :D
https://cdn...in your URL, not just//.... (2) The logic to replace thousands separators will only replace one thousands separator. If you have numbers in the millions, you will end up with a mis-formatted number - so tryreplaceAll()instead.