[英]Pound symbol (£) encoding issue while converting an HTML table into CSV using JavaScript
I am using pound symbol(£) in table head(th) and i am exporting the table in CSV format using javascript.我在表头(th)中使用英镑符号(£),并且我正在使用 javascript 以 CSV 格式导出表。 But the CSV file is showing the pound(£) symbol like " £ ".但是 CSV 文件显示了磅(£)符号,如“ £ ”。 I tried various solutions provided in SO threads.我尝试了 SO 线程中提供的各种解决方案。 But none of them worked for me.但他们都没有为我工作。 How can I fix this issue?我该如何解决这个问题?
Below is the code of the table:下面是表的代码:
<thead>
<tr>
<th>Property Name</th >
<th>Description</th >
<th>Category</th>
<th>Sub category</th>
<th>Amount</th>
<th>Unit</th>
<th> £ / Unit</th>
<th>Sub Total</th>
</tr>
</thead
I am using the below JavaScript function to export the table into CSV.我正在使用以下 JavaScript 函数将表导出为 CSV。
function exportTableToCSV(filename,userName) {
var csv = [];
var rows = document.querySelectorAll("table#"+userName+" tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
downloadCSV(csv.join("\n"), filename);
}
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {type: "text/csv"});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
The output is like the one in the below screenshot.输出类似于以下屏幕截图中的输出。
Can you replace js and check.你可以替换js并检查。
function exportTableToCSV(filename, userName) { var csv = []; var rows = document.querySelectorAll("table#" + userName + " tr"); for (var i = 0; i < rows.length; i++) { var row = [], cols = rows[i].querySelectorAll("td, th"); for (var j = 0; j < cols.length; j++) row.push(cols[j].innerText); csv.push(row.join(",")); } downloadCSV(csv.join("\\n"), filename); } function downloadCSV(csv, filename) { var csvFile; var downloadLink; var csvString = csv; var universalBOM = "\"; var a = window.document.createElement('a'); a.setAttribute('href', 'data:text/csv; charset=utf-8,' + encodeURIComponent(universalBOM + csvString)); a.setAttribute('download', filename); window.document.body.appendChild(a); a.click(); } exportTableToCSV('test.csv', 'velu');
<table id="velu"> <thead> <tr> <th>Property Name</th> <th>Description</th> <th>Category</th> <th>Sub category</th> <th>Amount</th> <th>Unit</th> <th> £ / Unit</th> <th>Sub Total</th> </tr> </thead> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.