gpt4 book ai didi

javascript - 导出为 CSV 时的编码问题

转载 作者:行者123 更新时间:2023-11-30 16:59:54 25 4
gpt4 key购买 nike

我有这段代码可以导出经过过滤的 html 表格。我有两个我无法弄清楚的问题:
1) 我在导出的 .csv 文件中得到“•”而不是项目符号点。我试过在元头和这个脚本中将字符集从 UTF-8 更改为 iso-8859,但这没有帮助。
2)当我在句子中有引号时,它会将其视为逗号并打断句子,将其放入下一个单元格中。示例:
我是一个有好名声的“好”人
会变成这样的:

A1 我是一个有
的“好”人B1口碑好

我搞不懂为什么会这样?
谢谢!

<script type='text/javascript'>//<![CDATA[ 

$(function(){

$('#downloadButton').click(function () {
var keepOnlyA1= true // put this true to remove unused numeric A0 field from each row [ A0, A1=[field,field,...]]
//
var a= tf_table1.GetFilteredData(true) // a= [ [A0,A1=[field,field,...]], [A0,A1=[field,field,...]], ...[..,[...]] ]
// ^a[0] ^a[1] ^a[n]
for (var i=0,row,r1; i<a.length; i++){ //
row= a[i] // row= [A0, A1=[field,field,...]]
r1= row[1] // ^r1
for (var j=0; j<r1.length; j++){ // surround each field with quotes "field"
r1[j]= '"'+r1[j].replace('"','""')+'"' // if there is any " already inside field string, it needs to be doubled. per csv rfc.
} //
if (keepOnlyA1) a[i]= r1 // if true, just replace each row array with its single interior A1=[field,field,...] array
} //
var colvals = a.join("\r\n") +"\r\n" //

var blob = new Blob([ colvals ], {type: 'text/csv;charset=ISO-8859;'});
//var filename = $('#fileName').val();

if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, "S7_Won_Tendes_Export_Filtered.csv");
}
else {
var a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.style.display= "none" //added// invisible
document.body.appendChild(a) //added// firefox wouldn't click() it without being appended
a.click();
//if (a.remove) a.remove();
a.parentNode.removeChild(a) //added//
}
});
});//]]>

</script>

最佳答案

好的,我设法找到了答案:

替换

var blob = new Blob([ colvals ], 

var blob = new Blob(["\uFEFF" + buffer], 

r1[j]= '"'+r1[j].replace('"','""')+'"'

r1[j]= '"'+r1[j].replace(/"/g, '""')+'"'

关于javascript - 导出为 CSV 时的编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com