“Ekspor CSV Single JavaScript” Kode Jawaban

Buat JavaScript File 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

Buat JavaScript File 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

Ekspor CSV Single JavaScript

//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

Jawaban yang mirip dengan “Ekspor CSV Single JavaScript”

Pertanyaan yang mirip dengan “Ekspor CSV Single JavaScript”

Lebih banyak jawaban terkait untuk “Ekspor CSV Single JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya