如何更改“逗号"?分隔符到blob csv中的其他内容? [英] How to change "comma" separator to something else in blob csv?
本文介绍了如何更改“逗号"?分隔符到blob csv中的其他内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用下面的jsfiddle代码来分隔代码以创建csv文件.它可以正常工作,除非列文本中有逗号.如何更改分隔符?
I am using the below jsfiddle code to separate the code to create csv file.It works fine except when there is comma in my column text.How can I change the separator?
function makeCSV() {
var csv = "";
$("table").find("tr").each(function () {
var sep = "";
$(this).find("input").each(function () {
csv += sep + $(this).val();
sep = ",";
});
csv += "\n";
});
$("#csv").text(csv);
window.URL = window.URL || window.webkiURL;
var blob = new Blob([csv]);
var blobURL = window.URL.createObjectURL(blob);
$("#downloadLink").html("");
$("<a></a>").
attr("href", blobURL).
attr("download", "data.csv").
text("Download Data").
appendTo('#downloadLink');
}
推荐答案
我将bolb数据更改为字符串,因此可以更改所有的,":
I changed the bolb data to string, so you can change all the ",":
const data = ["a", "b", "c"];
const exportData = data.toString().replace(/,/g, '\n');
const blob = new Blob([exportData], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.setAttribute('visibility', 'hidden');
link.download = 'test.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
这篇关于如何更改“逗号"?分隔符到blob csv中的其他内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文