Aman Kaushik
Aman Kaushik

Reputation: 23

Datatables excel Export: Single Column type as text

I have been working on datatables jquery plugin and on exporting the datatable to excel, numbers in a particular column are displayed in the exponential notation. How can i convert the data to text for one column only?

Upvotes: 0

Views: 2435

Answers (2)

Aman Kaushik
Aman Kaushik

Reputation: 23

I found my way around this. Here is what I did

var table = $('#example').DataTable({
                dom: 'BLfrtip',
                pageLength: 50,
                buttons: [{
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible'
                    },
                Text: 'Export To Excel',
                filename: 'Transaction Report',                      
                customizeData: function (data) {
                    for (var i = 0; i < data.body.length; i++) {
                       for (var j = 0; j < data.body[i].length; j++) {
                            if (data.header[j] == "Column Name") {
                                data.body[i][j] = '\u200C' + data.body[i][j];
                            }
                        }
                    }
                }}]});

This adds double quotes to each entry in the column

Upvotes: 1

Abhijeet Kale
Abhijeet Kale

Reputation: 389

By using exportOptions property of jquery datatable plugin you can restrict columns from exporting.

$('#myTable').DataTable( {
  buttons: [
  {
    extend: 'excelHtml5',
    text: 'Save current page',
    exportOptions: {
     columns: 0,1,2 //it will exporting only 3 columns out of n no of columns
    }
  }
]
});

By using this you can avoid exporting of problematic column !!

Upvotes: 0

Related Questions