【问题标题】:jQuery export to Excel, include html th rowsjQuery 导出到 Excel,包括第 html 行
【发布时间】:2013-11-26 11:59:52
【问题描述】:

我有小提琴,从另一个使用 jQuery 将 html 表导出到 CSV 的论坛修改。这非常有效,但是我无法让它包含表格标题行。

小提琴在: http://jsfiddle.net/KPEGU/480/

我搞砸了下面的语法,没有任何运气:

var $rows = $table.find('tr:has(td)'),

有什么关系吗:

$cols = $row.find('td');

所以我正在寻找包含标题的输出,而不仅仅是普通的表格行。

完整的脚本是:

$(document).ready(function () {

    function exportTableToCSV($table, filename) {

        var $rows = $table.find('tr:has(td)'),


            tmpColDelim = String.fromCharCode(11), 
            tmpRowDelim = String.fromCharCode(0), 
            colDelim = '","',
            rowDelim = '"\r\n"',

            csv = '"' + $rows.map(function (i, row) {
                var $row = $(row),
                    $cols = $row.find('td');

                return $cols.map(function (j, col) {
                    var $col = $(col),
                        text = $col.text();

                    return text.replace('"', '""'); 
                }).get().join(tmpColDelim);

            }).get().join(tmpRowDelim)
                .split(tmpRowDelim).join(rowDelim)
                .split(tmpColDelim).join(colDelim) + '"',

            // Data URI
            csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

        $(this)
            .attr({
            'download': filename,
                'href': csvData,
                'target': '_blank'
        });
    }


    $(".export").on('click', function (event) {
        // CSV
        exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);

    });
});

【问题讨论】:

    标签: jquery html html-table export-to-csv


    【解决方案1】:

    几个小改动:

    var $rows = $table.find('tr:has(td,th)'),
    

    $cols = $row.find('td,th');
    

    【讨论】:

    • 冠军谢谢。我正在尝试 tr:has(td),tr:has(th)。再次感谢,不胜感激。
    • 是的,它有效 - 我现在在我的项目中使用它jsfiddle.net/KPEGU/2960
    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多