Ravi Kanth
Ravi Kanth

Reputation: 1210

How to export a table with header to Excel with table2excel

I'm using table2excel to export a datatable powered by jQuery DataTables to Excel. I am able to export the data to excel but i am not able to export table with the heading.

Here is my code

$('#btn-export').on('click', function () {
   $('<table>').append(table.$('tr').clone()).table2excel({
      exclude: "",
      name: "EmailTracking",
      filename: "EmailTracking" //do not include extension
   });
});

Here is my HTML:

<table id="userGrid" class="table table-striped table-bordered dt-responsive nowrap " cellspacing="0" width="100%">
    <thead>
        <tr>
          <th>User</th>
           <th>Subject</th>
           <th>Sent On</th>
           <th>To</th>
           <th>Cc</th>
           <th>Bcc</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

Upvotes: 1

Views: 7562

Answers (2)

Aghnat Atqiya
Aghnat Atqiya

Reputation: 295

change your html code to this code:

<table id="userGrid" class="table table-striped table-bordered dt-responsive nowrap " 
cellspacing="0" width="100%">
    <thead>
        <tr>
          <td><b>User</b></td>
          <td><b>Subject</b></td>
          <td><b>Sent On</b></td>
          <td><b>To</b></td>
          <td><b>Cc</b></td>
          <td><b>Bcc</b></td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

Upvotes: 0

Gyrocode.com
Gyrocode.com

Reputation: 58890

Use the code below:

$('<table>')
  .append($(table.table().header()).clone())
  .append(table.$('tr').clone())
  .table2excel({
     exclude: "",
     name: "EmailTracking",
     filename: "EmailTracking" //do not include extension
  });

See this example for code and demonstration.

Upvotes: 5

Related Questions