“Exportar JavaScript único CSV” Respostas de código

Crie JavaScript de arquivo CSV

 $("#download_1").click(function() {
var json_pre = '[{"Id":1,"UserName":"Sam Smith"},{"Id":2,"UserName":"Fred Frankly"},{"Id":1,"UserName":"Zachary Zupers"}]';
var json = $.parseJSON(json_pre);

var csv = JSON2CSV(json);
var downloadLink = document.createElement("a");
var blob = new Blob(["\ufeff", csv]);
var url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = "data.csv";

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
Horrible Horse

Crie JavaScript de arquivo CSV

function JSON2CSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';
    var line = '';

    if ($("#labels").is(':checked')) {
        var head = array[0];
        if ($("#quote").is(':checked')) {
            for (var index in array[0]) {
                var value = index + "";
                line += '"' + value.replace(/"/g, '""') + '",';
            }
        } else {
            for (var index in array[0]) {
                line += index + ',';
            }
        }

        line = line.slice(0, -1);
        str += line + '\r\n';
    }

    for (var i = 0; i < array.length; i++) {
        var line = '';

        if ($("#quote").is(':checked')) {
            for (var index in array[i]) {
                var value = array[i][index] + "";
                line += '"' + value.replace(/"/g, '""') + '",';
            }
        } else {
            for (var index in array[i]) {
                line += array[i][index] + ',';
            }
        }

        line = line.slice(0, -1);
        str += line + '\r\n';
    }
    return str;
}
Horrible Horse

Exportar JavaScript único CSV

//EXPORT SINGLE COLUMN CSV JAVASCRIPT
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {type: "text/csv"});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
//export
function exportss(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
let aa = Array.from(rows).map((x,y)=> {
var row = [], cols = rows[y].querySelectorAll("td, th"); 
    Array.from(cols).forEach((a,b)=>{
        row.push(cols[b].innerText);
    })
    csv.push(row.join(","))
    console.log(csv)   
    return x
    })
downloadCSV(csv.join("\n"), filename);
}
Maeron Reyes

Respostas semelhantes a “Exportar JavaScript único CSV”

Perguntas semelhantes a “Exportar JavaScript único CSV”

Mais respostas relacionadas para “Exportar JavaScript único CSV” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código