我正在使用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>
答案 0 :(得分:3)
使用以下代码:
$('<table>')
.append($(table.table().header()).clone())
.append(table.$('tr').clone())
.table2excel({
exclude: "",
name: "EmailTracking",
filename: "EmailTracking" //do not include extension
});
请参阅this example以获取代码和演示。
答案 1 :(得分:0)
将您的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>