使用以下代码进行导出时:JSFiddle
HTML:
<div id="dvData">
<table>
<tr>
<th>Column One</th>
<th>Column Two</th>
<th>Column Three</th>
</tr>
<tr>
<td>€ 100,0</td>
<td>row1 Col2</td>
<td>row1 Col3</td>
</tr>
<tr>
<td>€ 120,99</td>
<td>row2 Col2</td>
<td>row2 Col3</td>
</tr>
<tr>
<td>€ 120,99</td>
<td>row3 Col2</td>
<td>row3 Col3</td>
</tr>
</table>
</div>
使用Javascript:
$(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
});
});
输出如下:
但我希望输出看起来像:
我怎样才能实现这一目标? charset设置为'UTF-8'。 UTF-8包含欧元符号吗?那么为什么我得到â,作为输出?
答案 0 :(得分:0)
你是使用UTF-8编码的html文件吗?
如果你正在使用另一个,你可以得到这个结果