Back

Export Excel file from HTML table

Copy Below Code View As A Text File Show Text Only Show API Edit Code
                            

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.4/xlsx.full.min.js"></script> <title>Document</title> </head> <body> <button onclick="exportTableToExcel('myTable', 'myExcelFile')">Export to Excel</button> <table id="myTable"> <thead> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>25</td> <td>New York</td> </tr> <tr> <td>Alice</td> <td>30</td> <td>Los Angeles</td> </tr> <tr> <td>Bob</td> <td>28</td> <td>Chicago</td> </tr> </tbody> </table> <script> function exportTableToExcel(tableId, fileName) { var table = document.getElementById(tableId); var wb = XLSX.utils.table_to_book(table, { sheet: fileName }); XLSX.writeFile(wb, fileName + '.xlsx'); } </script> </body> </html>