我写了一个以各种格式导出数据的演示:
HTML code:
<div class="container" style="position: relative;top:60px;">
<div class="row">
<table id="empTable" class="display table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Office</th>
<th>Extension</th>
<th>Joining Date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Office</th>
<th>Extension</th>
<th>Joining Date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
</div>
</div>
Javascript代码:
var editor;
$('#empTable').dataTable({
dom: 'Bfrtip',
"ajax": "empdata.json",
"columns": [{
"data": "name"
}, {
"data": "designation"
}, {
"data": "office"
}, {
"data": "extension"
}, {
"data": "joining_date"
}, {
"data": "salary"
}],
select: true,
buttons: [
{
extend: 'collection',
text: 'Export',
buttons: [
'copy',
'excel',
'csv',
'pdf',
'print'
],
}]
/*buttons: [{
extend: 'create',
editor: 'myEditor',
formButtons: [{
label: 'cancel',
fn: function() {
this.close();
}
}]
}]*/
});
这是jsfiddle链接:https://jsfiddle.net/3x0pa64o/
当我以各种格式下载表格数据时,它会以blob
的形式下载,而不是csv
,pdf
,excel
...这是什么原因?
答案 0 :(得分:2)
不确定你的意思&#34;它下载为blob,但不是csv,pdf,excel&#34;。它只是缺少示例中的文件名。
如https://datatables.net/extensions/buttons/examples/html5/filename.html和https://datatables.net/extensions/buttons/examples/flash/filename.html
中所述默认情况下,由...按钮类型创建的文件名将自动从文档的title元素中获取。
所以你在title
元素中设置文件名: