datatable excel export code example

Example 1: datatable csv export

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );

Example 2: datatable export bar

$(document).ready(function () {
  $("#example").DataTable({
    dom: "lBfrtip",
    columnDefs: [{ orderable: false, targets: 0 }],
    select: true,
    buttons: [
      "pageLength",
      {
        extend: "excel",
        exportOptions: {
          columns: ":visible",
        },
      },
      {
        extend: "csv",
        exportOptions: {
          columns: ":visible",
        },
      },
      {
        extend: "pdf",
        exportOptions: {
          columns: ":visible",
        },
      },
      {
        extend: "print",
        text: "Print all",
        exportOptions: {
          columns: ":visible",
        },
      },
      {
        extend: "print",
        text: "Print selected",
        exportOptions: {
          columns: ":visible",
        },
      },
      "colvis",
    ],
  });
});

Tags:

Misc Example