how to write data into csv file in javascript?

how to write data into csv file in javascript – Comma Separated Value or CSV is the data format that we encounter most frequently to import and input data into a massive database. A comma-separated values (CSV) file is a plain text file that stores tabular data.

how to write data into csv file in javascript

Create and download data in CSV format using plain JavaScript.

write csv file in javascript

JavaScript create and download CSV file
index.html

  
  
 Download CSV file   
  
  
  
  
  

Click the button to download the CSV file

create csv file javascript

function JSON2CSV(objArray) {
    var bulkSMS = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var usrStr = '';
    var queStr = '';

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

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

    for (var i = 0; i < bulkSMS.length; i++) {
        var queStr = '';

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

        queStr = queStr.slice(0, -1);
        usrStr += queStr + '\r\n';
    }
    return usrStr;
}

Don't Miss : Export HTML Table Data To CSV Using JQuery

I hope you get an idea about how to write data into csv file in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment