Is it possible to generate excel / csv or pdf from certain columns of a DataTables table?

2

I have a Table:

                            <table  class="tabelaRelatorioPausas table table-condensed table-striped  table-hover table-responsive " cellspacing="0" width="100%" >
                            <thead>
                            <th><i class="fa fa-headphones" ></i> Agente</th>
                            <th ><i class="fa fa-user-circle" ></i> Ramal</th>
                            <th ><i class="fa fa-clock-o" ></i> Almoço</th>
                            <th ><i class="fa fa-clock-o" ></i> Intervalo</th>
                            <th ><i class="fa fa-clock-o" ></i> Outro</th>
                            <th ><i class="fa fa-clock-o" ></i> Total</th>
                            <th ><i class="fa fa-clock-o" ></i> Zerar</th>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>

and the javascript code of the DataTable:

 $('.tabelaRelatorioPausas').DataTable({
            dom: 'lfBrtip',
            buttons: [
                'csv', 'excel', 'pdf'
            ],
            "oLanguage": {
                "sLengthMenu": "Mostrando _MENU_ registros",
                "sZeroRecords": "Nenhum registro encontrado",
                "sInfo": "Mostrando _START_ / _END_ de _TOTAL_ registro(s)",
                "sInfoEmpty": "Mostrando 0 / 0 de 0 registros",
                "sInfoFiltered": "(filtrado de _MAX_ registros)",
                "sSearch": "Pesquisar: ",
                "oPaginate": {
                    "sFirst": "Início",
                    "sPrevious": "Anterior",
                    "sNext": "Próximo",
                    "sLast": "Último"
                }
            }
        });
  

You can remove the last column (Zerar) by clicking the buttons button to generate report ( 'csv', 'excel', 'pdf'

asked by anonymous 14.08.2017 / 22:03

1 answer

1

According to the DataTables documentation :

  

The exportOptions.columns option of the print button provides the utility of selecting only certain columns (using column-selector ). Free translation, by myself

$('.tabelaRelatorioPausas').DataTable({
    dom: 'lfBrtip',
    buttons: [
        // Isto pode ser replicado para outros botões
        {
            extend: 'excel',
            text: 'Exportar MS Excel',
            exportOptions: {
                // Aqui você inclui o índice da coluna
                // No caso, eu peguei as colunas "Agente", "Ramal" e "Intervalo"
                // Sabendo que os índices começam com 0
                columns: [0, 1, 3] 
            }
        }, {
            extend: 'pdf',
            text: 'Exportar <em>PDF</em>',
            exportOptions: {
                // Exporta "Agente", "Ramal" e "Almoço"
                columns: [0, 1, 2] 
            }
        }
    ],
    "oLanguage": {
        "sLengthMenu": "Mostrando _MENU_ registros",
        "sZeroRecords": "Nenhum registro encontrado",
        "sInfo": "Mostrando _START_ / _END_ de _TOTAL_ registro(s)",
        "sInfoEmpty": "Mostrando 0 / 0 de 0 registros",
        "sInfoFiltered": "(filtrado de _MAX_ registros)",
        "sSearch": "Pesquisar: ",
        "oPaginate": {
            "sFirst": "Início",
            "sPrevious": "Anterior",
            "sNext": "Próximo",
            "sLast": "Último"
        }
    }
});

More references:

Column Selectors

Excel

File export

Format output data - export options p>     

15.08.2017 / 00:17