I know the post is old but as I have not seen any answer that I consider correct, here I leave my vision.
From what I understand you want to take the format of "8.50" and go to "R$8,50", and then export the table to Excel. Let’s go by part then:
1) From "8.50" to "R$8.50"
This type of procedure should be done using the property of the Datatable itself 'columnDefs' and not 'forcing' through an external rendering because otherwise vc will not be able for example to have the Data table do the sum of the values or export to excel or pdf with the correct mask.
In your code look for the property 'columnDefs', if you have not created can create, it will be like this
-- ...codigo anterior do DataTable---
pageLength: 50,
responsive:true,
data: dataSet,
columnDefs: [
{ "width": "7%", "targets": 0 }, //defini manualmente o tamanho da coluna
{ "width": "5%", "targets": 1 }, //defini manualmente o tamanho da coluna
{ "width": "5%", "targets": 2 }, //defini manualmente o tamanho da coluna
{ "width": "7%", "targets": 3 }, //defini manualmente o tamanho da coluna
{ "width": "7%", "targets": 4 }, //defini manualmente o tamanho da coluna
{"render": function(data){ //responsavel por 'renderizar' a coluna que vc deseja aplicando o filtro R$
return parseFloat(data).toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});
},"targets": 4
}
],
columns: [
{ title: "Tipo" },
{ title: "NºDoc" },
{ title: "Pedido" },
{ title: "Emissão" },
{ title: "Valor" }
]
--.... resto do código do seu DataTable ....--
Note that the part responsible for applying the filter is inside the property 'columnDefs', 'render'.
{"render": function(data){ //responsavel por 'renderizar' a coluna que vc deseja aplicando o filtro R$
return parseFloat(data).toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});
},"targets": 4
}
Here you correctly apply the mask and use the 'targets' command to point to which column you want to be applying
*BS:*If you wanted to apply this formatting to multiple columns would you just put the columns q you want in array, like this
{"render": function(data){ //responsavel por 'renderizar' a coluna que vc deseja aplicando o filtro R$
return parseFloat(data).toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});
},"targets": [4,5,8]
}
Remembering q the first column vc counts 0, because we are working with array here
2)Execel export button
I don’t know how the export button was made... but I do it this way:
--- ... código do dataTable ...--
columns: [
{ title: "Tipo" },
{ title: "NºDoc" },
{ title: "Pedido" },
{ title: "Emissão" },
{ title: "Valor" }
],
dom: 'Bfrtip',
altEditor: true,
buttons: [
{
text: 'EXCEL',
extend: 'excel',
exportOptions: {
modifier: {
page: 'current'
}
}
},
]
-- ...resto do código do DataTable...---
And last but not least, remember that you must import the relevant libraries if you do not have, if you decide to do this via Cdn will stay like this:
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.6.1/css/buttons.dataTables.min.css"/> <!-- dataTables.css buttons -->
<script src="https://cdn.datatables.net/buttons/1.6.1/js/dataTables.buttons.min.js"></script> <!-- dataTables.js buttons -->
<script src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.html5.min.js"></script> <!-- dataTables.html5 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script> <!-- dataTables.pdf maker -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script> <!-- dataTables.font maker -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> <!-- dataTables.xlsx -->
Ready! it seems a lot but it is not. With this you applied the money mask as you send the documentation and is free to manipulate the values of the column without worrying about it.
When you do replace, it generates an error, or simply does not get in the format you want?
– Wictor Chaves
It goes to excel as "General" format, so when I use the sum function, it cannot add up. I wanted to export with accounting format
– Elcio Santos
Because you have put a code, in which you use replace, does this replace work? Or does this modification not work?
– Wictor Chaves
This code I put from replace would be to convert the text 8.5 for example to 8.5, so it is possible to transform the text into a float. What I wanted now was to turn this float into a grind when generating excel.
– Elcio Santos