Export Html Table To Excel Spreadsheet using jQuery - table2excel

File Size: 236 KB
Views Total: 639994
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Export Html Table To Excel Spreadsheet using jQuery - table2excel

table2excel is a jQuery plugin for downloading HTML table data as Excel-compatible XLS files or XLSX workbooks.

Features:

  • Exclude table rows or cells with a CSS selector.
  • Preserve background and font colors in XLS files.
  • Retain table headers and row/column spans in legacy XLS output.
  • Generate XLSX workbooks with optional cell borders.
  • Control how images, links, and inputs are handled in XLS exports.
  • Set custom download filenames and worksheet names.

How to use it:

1. Download and load the jquery.table2excel.min.js after jQuery. Include JSZip if you plan to export XLSX files.

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="dist/jquery.table2excel.min.js"></script>

2. Add an HTML table and two export buttons. The noExl row will be excluded from both exports.

<button type="button" id="export-xls">Export XLS</button>
<button type="button" id="export-xlsx">Export XLSX</button>

<table id="table2excel">
  <thead>
    <tr>
      <th style="background-color: #dce7f1; color: #263645;">Product</th>
      <th>Quantity</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Keyboard</td>
      <td>2</td>
      <td>49.00</td>
    </tr>
    <tr>
      <td>Mouse</td>
      <td>3</td>
      <td>19.00</td>
    </tr>
    <tr class="noExl">
      <td>Internal note</td>
      <td colspan="2">Excluded from export</td>
    </tr>
  </tbody>
</table>

3. Bind the XLS export button. This HTML-based format can trigger an Excel file-format warning. preserveColors: true retains computed background and text colors.

$(function() {
  $("#export-xls").on("click", function() {
    $("#table2excel").table2excel({
      exclude: ".noExl",
      name: "Sales",
      filename: "sales-report.xls",
      preserveColors: true
    });
  });
});

Export XLSX with cell borders:

Set borders: true to generate an XLSX workbook with thin cell borders. This export writes cell text and omits HTML colors and merged cells.

$(function() {
  $("#export-xlsx").on("click", function() {
    $("#table2excel").table2excel({
      exclude: ".noExl",
      name: "Sales",
      filename: "sales-report.xlsx",
      borders: true
    });
  });
});

All Plugin Options:

Option Description
exclude Selector for excluded rows or cells. Default: ".noExl".
name XLSX worksheet name unless sheetName is set. Default: "Table2Excel".
filename Download name. Default: "table2excel". Include .xls for XLS. XLSX export adds .xlsx as needed.
fileext Legacy setting, default ".xls". Specify the download extension in filename.
exclude_img Replace images with their alt text in XLS. Default: true.
exclude_links Remove link markup and keep link text in XLS. Default: true.
exclude_inputs Replace inputs with their HTML value attributes in XLS. Default: true.
preserveColors Preserve row and cell background and font colors in XLS. Default: false.
borders Add thin cell borders and generate XLSX. Default: false.
excelFormat Select "xls" or "xlsx". Default: "xls". Use "xlsx" for borderless XLSX output.
sheetName Override the worksheet name used by the XLS or XLSX exporter.
$(function() {
  $("#export-xlsx").on("click", function() {
    $("#table2excel").table2excel({
      exclude: ".noExl",
      name: "Table2Excel",
      filename: "table2excel",
      fileext: ".xls",
      exclude_img: true,
      exclude_links: true,
      exclude_inputs: true,
      preserveColors: false,
      borders: false,
      excelFormat: "xls" // "xls" or "xlsx"
    });
  });
});

Alternative plugins:

See also:

Changelog:

2026-11-06

  • Updated documentation.

2026-01-14

  • Add basic border support

2019-06-04

  • Added support for background colors and font colors in rows and columns.

2019-02-07

  • v1.1.2: Update jquery.table2excel.js

2017-05-26

  • v1.1.1: Update jquery.table2excel.js

2016-03-08

  • Support unicode and file extension feature.

2015-05-22

  • Update jquery.table2excel.js

2015-04-08

  • Added IE11 support

2015-04-04

  • Changes made to move toward supporting multiple tables to single Excel file.

This awesome jQuery plugin is developed by rainabba. For more Advanced Usages, please check the demo page or visit the official website.