【问题标题】:How to export a table with header to Excel with table2excel如何使用 table2excel 将带有标题的表格导出到 Excel
【发布时间】:2016-07-20 06:29:44
【问题描述】:

我正在使用 table2excel 将由 jQuery DataTables 提供支持的数据表导出到 Excel。我可以将数据导出到 Excel,但我无法导出带有标题的表格。

这是我的代码

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

这是我的 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>

【问题讨论】:

标签: javascript jquery html excel datatables


【解决方案1】:

使用下面的代码:

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

有关代码和演示,请参阅this example

【讨论】:

    【解决方案2】:

    将您的 html 代码更改为以下代码:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2018-07-03
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 2011-09-27
      相关资源
      最近更新 更多