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
Contents
Create and download data in CSV format using plain JavaScript.
write csv file in javascript
JavaScript create and download CSV file
index.html
<html> <head> <title> Download CSV file </title> </head> <script> //create CSV file data in an array var csvFileData = [ ['Mayur Jadav', 'Player'], ['Krishna Durga', 'Batsmen'], ['Margi Dave', 'bolwer'], ['anupam Gacve', 'Player'], ['Rohit Sharma', 'Dancer'] ]; //create a user-defined function to download CSV file function getAllDownloadFile() { //define the heading for each row of the data var csv = 'Name,Pakainfo_v1\n'; //merge the data with CSV csvFileData.forEach(function(row) { csv += row.join(','); csv += "\n"; }); //display the created CSV data on the web browser document.write(csv); var hiddenElement = document.createElement('a'); hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv); hiddenElement.target = '_blank'; //provide the name for the CSV file to be downloaded hiddenElement.download = 'Famous Personalities.csv'; hiddenElement.click(); } </script> <body> <h3> Click the button to download the CSV file </h3> <!-- create an HTML button to download the CSV file on click --> <button onclick="getAllDownloadFile()"> Download CSV </button> </body> </html>
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.