将表格导出为CSV – 将string格式设置为单独的单元格

我有几个HTML表格,我想让用户将其内容导出到CSV。

我目前已经实施了这个解决scheme,它几乎可以完美地工

function exportTableToCsv($table, filename) { var $rows = $table.find('tr:has(td,th)'), 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,th'); 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' }); } 

我这样称呼:

 $(".ExportSummary").on('click', function () { exportTableToCsv.apply(this, [$('#SummaryTable'), 'ExportSummary.csv']); }); 

小提琴的例子


现在, 我的问题是 ,我不能通过将单独的单元格中的<td>放在Excel中的string格式工作。 我只是不知道如何让文本被放置在分离的单元格中,因为它被映射在一起成为一个完整的string内容。

我想要这个JsFiddle提供的所需输出 – 但是这个解决scheme不能提供select文件名和设置适当的内容types(application / csv)被浏览器识别的能力。

任何帮助表示赞赏。 提前致谢!

http://en.wikipedia.org/wiki/Comma-separated_values#Example

USA / UK CSV文件的小数点分隔符是句号/句号,值分隔符是逗号。 欧洲CSV / DSV文件的小数点分隔符是逗号,值分隔符是分号

我修改了一下你的脚本:

 function exportTableToCsv($table, filename) { ... // 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,th'); return $cols.map(function (j, col) { var $col = $(col), text = $col.text(); ... 

http://jsfiddle.net/mu5g1a7x/2/

让我知道,如果我正确地理解你。