HTML Tables to Excel Documentation
The exportTable
, addExport
, and setupExport
functions lets you easily add functionality to your pages to export HTML tables to Excel.
Getting Started
To get started, follow the steps below to start using the export script inside your project.
-
Import
export.js
into your project. In case you’d like to use CSS styles for the tables and button, you can find them insidestyle.css
-
In your main script, add the following lines to init the export functionality:
-
Mark your HTML tables in your markup with the
data-export
attribute where you’d like to display an Export button:
Alternatively, you can run the following steps to test out the functionality in the bundle, outside of your project directory:
-
Run
npm i
inside the project directory to install dependencies -
Run
npm run dev
to start the development server. Visitlocalhost
to see the project, and editmain.js
to play with the configurations. -
Optionally, Run
npm run build
to create a production version from the bundle
Make sure you init the export functionality after your DOM has fully loaded.
Export HTML Tables Programmatically
The script also supports triggering exports programmatically. For this, use the exportTable
function:
The function accepts any valid query selector string. The selector can also reference a DOM element. You can also define the exported file name through the second parameter. Be sure to specify the extension as xls
:
Add Export Button via JavaScript
It’s also possible to automatically include an Export button after your tables. For this, use the addExport
function in the following way:
Similar to the exportTable
function, addExport
expects a valid query string as the first parameter. The above function will only make the .my-table
element exportable. To make all tables on a page exportable, omit the selector, or pass all
:
The exported file’s name and the Export button’s label can also be configured through the addExport
function using the second and thrid parameters:
By default, the addExport
function uses the following settings:
selector
:'all'
file
:'table.xls'
button
:'Export'
Add Export Button via HTML attributes
The simplest way to integrate the table export functionality into your application is using the setupExport
function. You only need to call this function once, then mark your HTML tables with the data-export
attribute to make them exportable:
When using the setupExport
function, you’ll need to mark tables as exportable using the data-export
attribute:
The default file name and button text can also be overwritten per table using the data-file-name
and data-button-label
attributes.
Further Questions
Do you have questions not covered in this documentation? Please reach out to us using our contact form and we'll get back to you.
Back to product