If you're working with traditional web development and don't have a backend server, you can still use client-side libraries to convert an HTML table to Excel. Here are a couple of options that you can use in such scenarios:
-
TableExport.js: TableExport.js is a JavaScript library that enables exporting HTML tables to various formats, including Excel. It doesn't require a backend server and works entirely on the client-side.
<script src="https://unpkg.com/[email protected]/dist/js/tableexport.min.js"></script> <script> var table = document.getElementById('myTable'); TableExport(table, { formats: ['xlsx'], filename: 'output', }); </script>
You can include the above code in your HTML file, and it will add export functionality to the specified table. The resulting Excel file will be downloaded by the user.
-
SheetJS (client-side version): If you prefer to use SheetJS library on the client-side without a backend server, you can include the standalone version of the library in your HTML file.
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script> <script> function exportToExcel() { var table = document.getElementById('myTable'); var workbook = XLSX.utils.table_to_book(table); XLSX.writeFile(workbook, 'output.xlsx'); } </script>
In the above example, you need to define a function
exportToExcel()
that is called when you want to trigger the export. You can add a button or any other event to execute this function and export the table as an Excel file.
Both of these options work entirely on the client-side and do not require a backend server. Simply include the necessary libraries in your HTML file and utilize the provided functionalities to convert the HTML table to Excel format.