最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to export a table with header to Excel with table2excel - Stack Overflow

programmeradmin2浏览0评论

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>

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>
Share Improve this question edited Jul 20, 2016 at 14:35 Gyrocode. 59k16 gold badges156 silver badges192 bronze badges asked Jul 20, 2016 at 6:29 Ravi KanthRavi Kanth 1,21014 silver badges41 bronze badges 2
  • Better to use Jasper Report. – Ryan Commented Jul 20, 2016 at 7:12
  • datatables/forums/discussion/28188/… – Ryan Commented Jul 20, 2016 at 7:14
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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>
发布评论

评论列表(0)

  1. 暂无评论