Export HTML Table to Excel JavaScript Example

Export HTML Table to Excel JavaScript Example
Code Snippet:Export HTML Table Data into Excel File Using Js
Author: Bilal.Rizwaan
Published: May 11, 2024
Last Updated: May 11, 2024
Downloads: 47
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code example helps you export the HTML table to Excel. It selects and exports table content. It helps convert tables to Excel.

You can use this code on any webpage with an HTML table. It helps users export table data into Excel format easily. Enhances user experience by simplifying data management.

How to Export HTML Table to Excel Using JavaScript

1. First, load the following Bootstrap CDN link into the head tag of your HTML document.

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css'>

2. Ensure you have an HTML table on your webpage that contains the data you want to export. You can structure your table however you like, with headers and rows.

<div class="container my-5">
  <button class="btn btn-primary" id="btn">Export to Excel</button>
  <table id="Record" class="table table-striped table-dark my-3">
        <th scope="col">#</th>
        <th scope="col">First</th>
        <th scope="col">Last</th>
        <th scope="col">Handle</th>
        <th scope="row">1</th>
        <th scope="row">2</th>
        <th scope="row">3</th>
        <td>the Bird</td>
        <th scope="row">4</th>
        <td>the Bird</td>

3. Load the following Table2Excel CDN link before closing the body tag.

<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/table2excel.min.js'></script>

4. Add the following JavaScript code to your webpage. This code sets up an event listener on a button, which, when clicked, triggers the export of the HTML table data to Excel format.

document.getElementById("btn").addEventListener("click", () => {
  let table2excel = new Table2Excel();

You can further enhance this functionality by adding error handling, additional export options, or integrating it with server-side scripts for more complex data manipulation before export.

Congratulations! You’ve successfully implemented a JavaScript solution to export HTML table data to Excel, enhancing the usability and accessibility of your web application

Leave a Comment