Wrong format excel exporting
Wrong format excel exporting
arranzropablo
Posts: 6Questions: 1Answers: 0
Hello I'm trying to export to excel but the format for thousand separator is not behaving correctly... When it's a number under 1.000.000 it writes "," as separator, when it's higher than 1.000.000, it writes "." my purpose is having "." always as the thousand separator. This is my code in case it helps... Thank you!
`
this.initTable = function(tableId, options, filters) {
var filterEvents;
if (appname.tableGeneratorFilters && filters) {
filterEvents = appname.tableGeneratorFilters.init(filters);
}
var defaultOptions = {
dom: 'ri<"detail-actions"><"table-actions">Bf<"filters"><"live-filters">tlp',
buttons: [
{
extend: 'csv',
charset: 'UTF-16LE',
fieldSeparator: ';',
bom: true
},
{
extend: 'excel',
exportOptions: {
columns: ':visible',
format: {
body: function(data, row, column, node) {
var value = $('<span>' + data + '</span>').text();
return value;
}
}
},
filename: function(){
var d = new Date();
// TODO ... dd/mm/yyyy hh:mm:ss.
return document.title + ' ' +d.toISOString();
}
}
],
scrollX: true,
columnDefs: [
{
orderable: false ,
targets: "no-sort"
},
{
width: "1%",
targets: 0 }
],
order: [[ 1, 'asc' ]],
language: {
search: "_INPUT_",
searchPlaceholder: '',
url: ctx + "/js-i18n/tables-i18n.json"
}
};
var opts = $.extend({}, defaultOptions, options);
var table = $(tableId).DataTable(opts);`
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
PD: This is how I have it now, I tried adding language.thousands and didn't work... with this code is currently printing:
-for "656.019" it's printing "656,019" (wrong)
-for "58.323.791" it's printing "58.323.791" (right)
you need to use customization with the build-in style 63 for a number with thousand separators.
https://datatables.net/reference/button/excelHtml5
Like this for example if you would like an array of columns to be formatted like that:
Excel will then decide which thousand separator to use. In an English excel installation it will use a comma, in a German or Spanish installation it will use a period. So if you do the Excel export you don't preformat the number yourself but all you do is tell Excel to use a thousand separator. If you did the same for csv it would be opposite: You would need to anticipate the language and hence the Excel installation of your user and preformat everything yourself. For example for English users the value separator would be a comma for German users it would need to be a ";" etc. etc. Same applies to number formatting: For English users 1,000,000.00 for German and Spanish users 1.000.000,00
Yes - I'm afraid that at the moment, the Excel export only supports the British / American style of formatting numbers, not the European style.
That is something that I plan to address in a future version.
Allan
@allan: I tend to disagree - the Excel export is better than you think. The only thing @arranzropablo needs to change is to use built-in style 63 to explicitly tell Excel that the field has no decimals but thousand separators instead.
I am pretty sure @arranzropablo is using a Spanish Excel installation. Hence Excel interprets "656.019" as a decimal number (only one separator!) and converts into Spanish "656,019". Since Excel "knows" that a number can only have one decimal sign it interprets the periods in "58.323.791" to be thousand separators and displays the number correctly in the Spanish installation. If @arranzropablo were using an English Excel installation it would be exactly opposite. The numbers would be displayed in Excel as "656.019" and "58,323,791".
I tested this with a US and a German Excel installation.
I use this as a customization myself:
If you set my website to English numbers are passed to Excel as 1,000,000.00. Depending on what Excel installation you are using Excel displays them as 1,000,000.00 (English or US installation) or 1.000.000,00 (German installation).
Same applies if you set the site to German and 1.000.000,00 is passed to Excel. This does not matter at all. All that matters is the language setting in Excel - provided you use the built-in style.
Sorry I wasn't clear. Yes you are absolutely correct, Excel is excellent at internationalisation. What I was meaning was that Button's auto detection of the export will correctly detect US formatted numbers and then setup the spreadsheet for them, but not Euro formatted numbers.
DataTables does have a
language.thousands
andlanguage.decimal
option which will be used in future versions of Buttons to automatically support Euro styled numbers.This is the part of the Buttons code that does its type detection on output.
But yes, if you have a plain number, use Excel's built in styling options.
Allan
Thank you so much @rf1234 for your answer, but I'm afraid I can't apply that solution to the code I pasted... As I understand it, my code just tells the library to create two buttons with certain properties, but I can't obtain the
var sheet
at any moment as you do, to apply the.attr('s','63')
... if you could tell me where I should fit that, it would be a great help, thank you!sry I posted this accidentally
@allan so language.thousands and language.decimal are not working already? I'm still confused about how should I send my numbers... plain? "," separator? "." separator?
nevermind guys! I made it, thank you so much to both!
great you got it working! and also great that you will enhance auto detection, Allan.
Unfortunately my post was wrong. I overlooked that I use export options to strip everything out of the numbers passed to Excel. So I always pass 1000000.00 and not 1,000,000.00 or 1.000.000,00. This is the complete code of my custom Excel button if it helps at all. Everything I do for lang === 'de' would be the same for lang === 'es'.
The code has a couple of new styles because if you want to combine built-in styles with each other you need to define a proprietary style yourself. Combining by using two built-in styles does not work because one style overwrites the other one.