我有这个代码来导出过滤后的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>
答案 0 :(得分:2)
好的,我找到了答案:
替换
var blob = new Blob([ colvals ],
与
var blob = new Blob(["\uFEFF" + buffer],
和
r1[j]= '"'+r1[j].replace('"','""')+'"'
与
r1[j]= '"'+r1[j].replace(/"/g, '""')+'"'