jquery export to excel,include html th rows [英] jquery export to excel, include html th rows

查看:143
本文介绍了jquery export to excel,include html th rows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有小提琴,修改从另一个论坛导出html表到csv与jquery。这个工作完美,但我不能让它包括表标题行。

I have the fiddle, modified from another forum that exports an html table to csv with jquery. this works perfectly however I cannot get it to include the table header rows.

小提琴处于:
http://jsfiddle.net/KPEGU/480/

我搞错了

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

与以下内容有关:

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

所以我正在寻找输出包括标题,而不只是正常的表行。

So I am looking for output to include headings, not just normal table rows.

完成脚本是:

$(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']);

    });
});

提前感谢,一如既往。

推荐答案

一些小的变化:

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

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

这篇关于jquery export to excel,include html th rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆