使用&符编码CSV角度2的问题

时间:2017-08-11 10:25:05

标签: javascript angular csv typescript export-to-csv

我尝试使用Javascript下载CSV报告。但是,我在CSV中面临一些编码问题。得到&&以下是我的JS代码。我试过谷歌的解决方案,但无济于事。

JSONToCSVConvertor(ReportTitle, ShowLabel) {
    let csv = [];

    var tableId = document.querySelector('table').getAttribute('id');

    var headers = document.querySelectorAll("table#"+tableId+" tr");

    for (var i = 0; i < headers.length; i++) {

    var row = [], cols = headers[i].querySelectorAll("td, th");

    for (var j = 0; j < cols.length; j++)

        row.push(cols[j].innerHTML);


    csv.push(row.join(", "));   

}

this.downloadCSV(csv.join("\n"), sampleTitle);

downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    var uri = 'data:text/csv;charset=utf-8' + encodeURI(csv);

// Download link
    downloadLink = document.createElement("a");


    downloadLink.href = uri;

    downloadLink.download = filename + ".csv";

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();

    document.body.removeChild(downloadLink);
}

如何获取CSV中的确切特殊字符?有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

不确定这是否过度......

function encodeRFC5987ValueChars(str) {
    return window.encodeURIComponent(str)
        .replace(/['()]/g, window.escape)
        .replace(/\*/g, '%2A')
        .replace(/%(?:7C|60|5E)/g, window.unescape);
}