Skip to content

Instantly share code, notes, and snippets.

@opeyemidy
Created May 25, 2023 21:14
Show Gist options
  • Save opeyemidy/3ae48da1ca0519d578802f00a033ad96 to your computer and use it in GitHub Desktop.
Save opeyemidy/3ae48da1ca0519d578802f00a033ad96 to your computer and use it in GitHub Desktop.

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:

  1. 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/tableexport@5.2.0/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.

  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment