Guest User

Untitled

a guest
Apr 17th, 2018
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  3. <html>
  4. <body>
  5.  
  6. <table>
  7. <tr>
  8. <th>Name</th>
  9. <th>Email</th>
  10. <th>Country</th>
  11. </tr>
  12. <tr>
  13. <td>John Doe</td>
  14. <td>john@gmail.com</td>
  15. <td>USA</td>
  16. </tr>
  17. <tr>
  18. <td>Stephen Thomas</td>
  19. <td>stephen@gmail.com</td>
  20. <td>UK</td>
  21. </tr>
  22. <tr>
  23. <td>Natly Oath</td>
  24. <td>natly@gmail.com</td>
  25. <td>France</td>
  26. </tr>
  27. </table>
  28. <button onclick="exportTableToCSV('members.csv')">Export HTML Table To CSV File</button>
  29.  
  30. <script>
  31. function downloadCSV(csv, filename) {
  32. var csvFile;
  33. var downloadLink;
  34.  
  35. // CSV file
  36. csvFile = new Blob([csv], {type: "text/csv"});
  37.  
  38. // Download link
  39. downloadLink = document.createElement("a");
  40.  
  41. // File name
  42. downloadLink.download = filename;
  43.  
  44. // Create a link to the file
  45. downloadLink.href = window.URL.createObjectURL(csvFile);
  46.  
  47. // Hide download link
  48. downloadLink.style.display = "none";
  49.  
  50. // Add the link to DOM
  51. document.body.appendChild(downloadLink);
  52.  
  53. // Click download link
  54. downloadLink.click();// throwing error.
  55.  
  56. }
  57. function exportTableToCSV(filename) {
  58. var csv = [];
  59. var rows = document.querySelectorAll("table tr");
  60.  
  61. for (var i = 0; i < rows.length; i++) {
  62. var row = [], cols = rows[i].querySelectorAll("td, th");
  63.  
  64. for (var j = 0; j < cols.length; j++)
  65. row.push(cols[j].innerText);
  66.  
  67. csv.push(row.join(","));
  68. }
  69.  
  70. // Download CSV file
  71. downloadCSV(csv.join("n"), filename);
  72. }
  73.  
  74. </script>
  75.  
  76. </body>
  77. </html>
Add Comment
Please, Sign In to add comment