Can I generate an excel/csv or pdf from certain columns of a Datatables table?

Asked

Viewed 979 times

0

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 Datatable javascript code:

 $('.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 (Reset) by clicking on Buttons to report ( 'csv', 'excel', 'pdf') ?.
ps.:Only in PDF generation (for example), not in visualization.

1 answer

1


According to the datatables documentation:

The option exportOptions.Columns 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.