Javascript 如何将html表格导出到CSV

Javascript 如何将html表格导出到CSV,javascript,Javascript,我想将我的html表格导出到csv文件。我发现java脚本如下所示。正在下载表,但表中没有数据。谁能告诉我哪里出了问题。准确的代码在这里工作 你的小提琴很好用。我可以看到下载文件中的数据。fiddle对我有效唯一的问题是你的文件名没有被应用。原因是新的HTML5下载属性可以在BLOBURL或文件系统URL上工作。您必须将url转换为blob url。解决方法是:这看起来像是Chrome35中的一个bug。可能是 $(document).ready(function () { f

我想将我的html表格导出到csv文件。我发现java脚本如下所示。正在下载表,但表中没有数据。谁能告诉我哪里出了问题。准确的代码在这里工作


你的小提琴很好用。我可以看到下载文件中的数据。fiddle对我有效唯一的问题是你的文件名没有被应用。原因是新的HTML5下载属性可以在BLOBURL或文件系统URL上工作。您必须将url转换为blob url。解决方法是:这看起来像是Chrome35中的一个bug。可能是
$(document).ready(function () {

        function exportTableToCSV($table, filename) {

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

            // Temporary delimiter characters unlikely to be typed by keyboard
            // This is to avoid accidentally splitting the actual contents
            tmpColDelim = String.fromCharCode(11), // vertical tab character
            tmpRowDelim = String.fromCharCode(0), // null character

            // actual delimiter characters for CSV format
            colDelim = '","',
            rowDelim = '"\r\n"',

            // Grab text from table into CSV formatted string
            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('"', '""'); // escape double quotes

                }).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'
            });
        }

        // This must be a hyperlink

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

            // IF CSV, don't do event.preventDefault() or return false
            // We actually need this to be a typical hyperlink
        });
    });