DataTables Export to Excel

DataTables Export with some hidden columns

by Muhammad Mushtaq Sheikh

HTML

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
      <td>22</td>
      <td>2012/03/29</td>
      <td>$433,060</td>
    </tr>
    <tr>
      <td>Airi Satou</td>
      <td>Accountant</td>
     ...

CSS

body {
  margin: 15px;
}

JavaScript

$(document).ready(function() {
  var buttonCommon = {
    //exportOptions: {
    // format: {
    //    body: function ( data, row, column, node ) {
    // Strip $ from salary column to make it numeric
    //   return column === 5 ?
    //     data.replace( /[$,]/g, '' ) :
    //     data;
    //    }
    //  }
    // }
  };

  $('#example').DataTable({
    columns: [{
      data: 'name'
    }, {
      data: 'position'
    }, {
      data: 'office'
    }, {
      data: 'age',
      visible: false
    }, {
      data: 'start_date'
    }, {
      data: 'salary',
      visible: false
    }],
    dom: 'Bfrtip',
    buttons: [
      $.extend(true, {}, buttonCommon, {
        extend: 'copyHtml5'
      }),
      $.extend(true, {}, buttonCommon, {
        extend: 'excelHtml5'
      }),
      $.extend(true, {}, buttonCommon, {
        extend: 'pdfHtml5'
      }),
      $.extend(true, {}, buttonCommon, {
        extend: 'csvHtml5'
      })
    ]
  });
});