1var colors = ["red", "blue", "green"];
2var colorsString = colors.join(","); //"red,blue,green"
1const arrayToCSV = (arr, delimiter = ',') =>
2 arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
1 $("#download_1").click(function() {
2var json_pre = '[{"Id":1,"UserName":"Sam Smith"},{"Id":2,"UserName":"Fred Frankly"},{"Id":1,"UserName":"Zachary Zupers"}]';
3var json = $.parseJSON(json_pre);
4
5var csv = JSON2CSV(json);
6var downloadLink = document.createElement("a");
7var blob = new Blob(["\ufeff", csv]);
8var url = URL.createObjectURL(blob);
9downloadLink.href = url;
10downloadLink.download = "data.csv";
11
12document.body.appendChild(downloadLink);
13downloadLink.click();
14document.body.removeChild(downloadLink);
15});
1function exportToCsv(filename, rows) {
2 var processRow = function (row) {
3 var finalVal = '';
4 for (var j = 0; j < row.length; j++) {
5 var innerValue = row[j] === null ? '' : row[j].toString();
6 if (row[j] instanceof Date) {
7 innerValue = row[j].toLocaleString();
8 };
9 var result = innerValue.replace(/"/g, '""');
10 if (result.search(/("|,|\n)/g) >= 0)
11 result = '"' + result + '"';
12 if (j > 0)
13 finalVal += ',';
14 finalVal += result;
15 }
16 return finalVal + '\n';
17 };
18
19 var csvFile = '';
20 for (var i = 0; i < rows.length; i++) {
21 csvFile += processRow(rows[i]);
22 }
23
24 var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
25 if (navigator.msSaveBlob) { // IE 10+
26 navigator.msSaveBlob(blob, filename);
27 } else {
28 var link = document.createElement("a");
29 if (link.download !== undefined) { // feature detection
30 // Browsers that support HTML5 download attribute
31 var url = URL.createObjectURL(blob);
32 link.setAttribute("href", url);
33 link.setAttribute("download", filename);
34 link.style.visibility = 'hidden';
35 document.body.appendChild(link);
36 link.click();
37 document.body.removeChild(link);
38 }
39 }
40}
41
1function JSON2CSV(objArray) {
2 var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
3 var str = '';
4 var line = '';
5
6 if ($("#labels").is(':checked')) {
7 var head = array[0];
8 if ($("#quote").is(':checked')) {
9 for (var index in array[0]) {
10 var value = index + "";
11 line += '"' + value.replace(/"/g, '""') + '",';
12 }
13 } else {
14 for (var index in array[0]) {
15 line += index + ',';
16 }
17 }
18
19 line = line.slice(0, -1);
20 str += line + '\r\n';
21 }
22
23 for (var i = 0; i < array.length; i++) {
24 var line = '';
25
26 if ($("#quote").is(':checked')) {
27 for (var index in array[i]) {
28 var value = array[i][index] + "";
29 line += '"' + value.replace(/"/g, '""') + '",';
30 }
31 } else {
32 for (var index in array[i]) {
33 line += array[i][index] + ',';
34 }
35 }
36
37 line = line.slice(0, -1);
38 str += line + '\r\n';
39 }
40 return str;
41}
1const rows = [
2 ["name1", "city1", "some other info"],
3 ["name2", "city2", "more info"]
4];
5
6let csvContent = "data:text/csv;charset=utf-8,"
7 + rows.map(e => e.join(",")).join("\n");
8