CSV,PDF,Excel文件在dataTables的导出扩展名中作为blob下载

时间:2016-05-06 07:59:16

标签: javascript excel csv datatables

我写了一个以各种格式导出数据的演示:
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的形式下载,而不是csvpdfexcel ...这是什么原因?

1 个答案:

答案 0 :(得分:2)

不确定你的意思&#34;它下载为blob,但不是csv,pdf,excel&#34;。它只是缺少示例中的文件名。

https://datatables.net/extensions/buttons/examples/html5/filename.htmlhttps://datatables.net/extensions/buttons/examples/flash/filename.html

中所述

默认情况下,由...按钮类型创建的文件名将自动从文档的title元素中获取。

所以你在title元素中设置文件名:

示例:https://jsfiddle.net/qLqugynb/