Export html table into CSV file , always return empty file

那年仲夏 提交于 2019-12-02 04:19:53

I'll leave a working fiddle example at the bottom.

The part that needed some work was primarily:

exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);

To:

exportTableToCSV.apply(this, [$('#dvData'), 'export.csv']);

Then it was a matter of formatting the results:

// 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().replace(/(\r\n|\n|\r)/gm,"").trim() + '"';
                return text //.replace('"', '""'); // escape double quotes
            }).get().join(tmpColDelim);
        }).get().join(tmpRowDelim)
            .split(tmpRowDelim).join(rowDelim)
            .split(tmpColDelim).join(colDelim),

It now returns a comma separated list, where each column is enclosed with quotes.

Example on jsFiddle

I'll leave the fine tuning to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!