如何使Javascript/jQuery代码在csv文件中显示html表,而不考虑html格式?
您可能需要单击“导出”按钮两次才能显示csv文件 链接中的代码包含html、css和Javascript/jQuery。将数据导出到CSV的代码。代码工作得很好,但当我将表格格式设置为:如何使Javascript/jQuery代码在csv文件中显示html表,而不考虑html格式?,javascript,jquery,html,Javascript,Jquery,Html,您可能需要单击“导出”按钮两次才能显示csv文件 链接中的代码包含html、css和Javascript/jQuery。将数据导出到CSV的代码。代码工作得很好,但当我将表格格式设置为: <tr> <td> row1 Col1 </td> <td> row1 Col2 </td> <td>
<tr>
<td>
row1 Col1
</td>
<td>
row1 Col2
</td>
<td>
row1 Col3
</td>
</tr>
Javascript/jQuery:
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr:has(td,th)'),
// 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, th');
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
});
});
.excel格式适合您吗?您的代码运行得非常好。你说的“使表格格式为:row1 Col1 row1 Col2 row1 Col3”是什么意思?现在我明白了。。。您只需要从单元格内容中删除回车符。类似于text.replace(/(\r\n |\n |\r)/g,“”;请注意,并非表格的所有值都显示在csv文件中。它是格式化为:row1 Col1 row1 Col2 row1 Col3的html表部分。您可能还需要检查额外的空格。
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr:has(td,th)'),
// 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, th');
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
});
});