简体   繁体   中英

how to convert data json to xlsx in node js

I need your help. From the nodejs terminal, i want to download an excel file and convert data json to xlsx enter image description here

 var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var xl = '';
    if (ShowLabel) {
        var row = "";
        for (var index in arrData[0]) {
            row += index + ',';
        }
        row = row.slice(0, -1);
        xl += row + '\r\n';
    }
    for (var i = 0; i < arrData.length; i++) {
        var row = "";
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }
        row.slice(0, row.length - 1);
        xl += row + '\r\n';
    }
    if (xl == '') {
        this.alertService.error("Invalid data");
        return;
    }
    var fileName = "file_";
    fileName += ReportTitle.replace(/ /g, "_");
    var uri = 'data:text/xlsx;application/vnd.openxmlformats;charset=utf-8,' + encodeURI(xl);
    var link = document.createElement("a");
     link.href = uri;
    link.style.cssText = "visibility:hidden";
    link.download = fileName+".xlsx";
    document.body.appendChild(link);
    link.click(); 
    document.body.removeChild(link);

help my to convert data json to xlsx file using node js this code in valid in my system linux and not working in windows and mac problem extession

 var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var xl = '';
    if (ShowLabel) {
        var row = "";
        for (var index in arrData[0]) {
            row += index + ',';
        }
        row = row.slice(0, -1);
        xl += row + '\r\n';
    }
    for (var i = 0; i < arrData.length; i++) {
        var row = "";
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }
        row.slice(0, row.length - 1);
        xl += row + '\r\n';
    }
    if (xl == '') {
        this.alertService.error("Invalid data");
        return;
    }
    var fileName = "file_";
    fileName += ReportTitle.replace(/ /g, "_");
    var uri = 'data:text/xlsx;application/vnd.openxmlformats;charset=utf-8,' + encodeURI(xl);
    var link = document.createElement("a");
     link.href = uri;
    link.style.cssText = "visibility:hidden";
    link.download = fileName+".xlsx";
    document.body.appendChild(link);
    link.click(); 
    document.body.removeChild(link);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM