Amal
Amal

Reputation: 222

Retain decimal seperator on export excel in Datatables

This is my values that I am getting in datatable.

Cuenta  Nombre Cuenta       Oct18   Dec18   Sep19   Oct19   M$      M%  A$      A%
11      Activos Liquidos    5732,12 6950,89 5879,21 5965,02 85,82   1.4 232,91  4.06   

When exporting the excel the data's decimal place gets converted to 5732.12 6950.89 ..etc. I need to preserve the decimal I am using datatables.

I used this code:

{
      extend: 'excel',
      exportOptions: {
          columns: ':visible',
          format: {
              body: function(data, row, column, node) {
                  data = $('<p>' + data + '</p>').text();
                  return $.isNumeric(data.replace(',', '.')) ? data.replace(',', '.') : data;
              }
          }
      }
  }

But it changes my 4.06 to 4,06 which is incorrect. How to achieve this? Thank you.

Update :

This is the value I am getting in excel

This is what I am getting but completely wrong

Upvotes: 0

Views: 564

Answers (1)

mplungjan
mplungjan

Reputation: 178011

Does this help?

const fields = "11\tActivos Liquidos\t5732,12\t6950,89\t5879,21\t5965,02\t85,82\t1.4\t232,91\t4.06".trim().split("\t")
  .map(fld => /^\d/.test(fld) ? parseFloat(fld.replace(/,/,".")) : fld);
console.log(JSON.stringify(fields))

Upvotes: 1

Related Questions