Datatable change column names on excel export

Datatable change column names on excel export

ricardomartosricardomartos Posts: 1Questions: 1Answers: 0

Hello!
I'm new and I'm having trouble trying to change the names of columns when I export the table to Excel.

I have this:

            $('#TableTest').DataTable({
                autoWidth: false,
                processing: true,
                serverSide: true,
                ajax: {
                    url: "/web/a/info",
                    type: 'GET',
                    data: function( d ) {
                        d.value = $('#some_option option:selected').val();   
                    }
                },
                scrollCollapse: true,
                paging: true,
                jQueryUI: false,
                order: [ 1, 'asc' ],
                language: DTLang.language,
                bDestroy: true,
                columnDefs: [
                    {
                        "className": "text-center", "targets": "_all",
                        "targets":  [7] ,"render": function ( data) {return "<a href='/web/someurl/id/"+data+"/other/"+$('#other_option option:selected').val()+"'>Edit</a>";}
                    }
                ],
                selector:true,
                dom: 'Bflrtip',
                lengthMenu: [[10, 25, 50, 100, -1], [10,25, 50, 100, "All"]],
                buttons: [
                    {
                        extend: 'excelHtml5',
                        text: '<span class="fa fa-file-excel-o"></span>Excel',
                        title: null,
                        exportOptions: {
                            modifier: {
                                page: 'all'
                            },
                            columns: [0, 2, 3, 4, 5, 6]
                        }
                    }
                ]
            }); 

Could some one please help me? I'm getting seriously crazy.

Thanks.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓

    Hi take a look at this please: https://datatables.net/reference/button/excelHtml5

    Search for this text on that page please:
    "As an example, let's modify the text shown in cell A1:"

    That's what you would need to do!

    Here is an example from my own coding as part of the "customize" function:

    //move text from column B to column A and empty columns B through F of row with totals
    var copyPaste = $('row:eq(-2) c[r^="B"] t', sheet).text();
    $('row:eq(-2) c[r^="A"] t', sheet).text(copyPaste);
    var emptyCellCols = ['B', 'C', 'D', 'E', 'F'];
    for ( i=0; i < emptyCellCols.length; i++ ) {
        $('row:eq(-2) c[r^='+emptyCellCols[i]+']', sheet).text('');
    }
    
This discussion has been closed.