Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| <!DOCTYPE html> | |
| <html dir="ltr" lang="en-us"> | |
| <head> | |
| <meta charset="UTF-8"/> | |
| <title>Export to CSV using JavaScript</title> | |
| <!-- | |
| Author : Elia Contini <http://www.eliacontini.info> | |
| Created: 15 Mar 2014 | |
| License: see LICENSE file in the repository root | |
| --> | |
| </head> | |
| <body> | |
| <script> | |
| // some data to export | |
| var data = [ | |
| { | |
| "title": "Book title 1", | |
| "author": "Name1 Surname1" | |
| }, | |
| { | |
| "title": "Book title 2", | |
| "author": "Name2 Surname2" | |
| }, | |
| { | |
| "title": "Book title 3", | |
| "author": "Name3 Surname3" | |
| }, | |
| { | |
| "title": "Book title 4", | |
| "author": "Name4 Surname4" | |
| } | |
| ]; | |
| // prepare CSV data | |
| var csvData = new Array(); | |
| csvData.push('"Book title","Author"'); | |
| data.forEach(function(item, index, array) { | |
| csvData.push('"' + item.title + '","' + item.author + '"'); | |
| }); | |
| // download stuff | |
| var fileName = "data.csv"; | |
| var buffer = csvData.join("\n"); | |
| var blob = new Blob([buffer], { | |
| "type": "text/csv;charset=utf8;" | |
| }); | |
| var link = document.createElement("a"); | |
| if(link.download !== undefined) { // feature detection | |
| // Browsers that support HTML5 download attribute | |
| link.setAttribute("href", window.URL.createObjectURL(blob)); | |
| link.setAttribute("download", fileName); | |
| } | |
| else if(navigator.msSaveBlob) { // IE 10+ | |
| navigator.msSaveBlob(blob, fileName); | |
| } | |
| else { | |
| // it needs to implement server side export | |
| link.setAttribute("href", "http://www.example.com/export"); | |
| } | |
| link.innerHTML = "Export to CSV"; | |
| document.body.appendChild(link); | |
| </script> | |
| </body> | |
| </html> |