Table2Excel.js
This is a library to export html tables to excel sheets.
Precondition
It has to be a row * column table
Features
- able to export with width, alignment and colors
- extendable
Dependencies
Live Demo
Include table2excel.js
npm
ExcelJS
is peer dependency to table2excel.js
, so you need to install exceljs first:
npm i exceljs
then, install table2excel.js:
npm i table2excel.js
use in your code like:
'table'
you may also need a config in webpack:
node: fs: 'empty'
table2excel.min.js (with ExcelJS packed)
table2excel.core.js (without ExcelJS packed)
Basic Usage
const table2Excel = selector options // new Table2Excel('table')table2Excel // table2Excel.export('my-exported-table', 'xlsx')
extension
can be 'xls'
or 'xlsx'
, default as 'xlsx'
selector
It's optional, and defaulted as 'table'
options
It's optional, and defaulted as:
workbook: views: x: 0 y: 0 width: 10000 height: 20000 firstSheet: 0 activeTab: 1 visibility: 'visible' widthRatio: 14 plugins: Table2ExcelpluginsfontPlugin Table2ExcelpluginsfillPlugin Table2ExcelpluginsformPlugin Table2ExcelpluginsalignmentPlugin Table2ExcelpluginshyperlinkPlugin Table2ExcelpluginsautoWidthPlugin
workbook
is options used while creating a workbook, please refer exceljs#create-a-workbook for details.
widthRatio
is a ratio that will be used while converting width
style of html table cells to width of sheet cells.
Plugins
Plugin helps to extend the ability of transforming table to excel.
Build-in plugins can be access like:
Table2ExcelpluginsfontPluginTable2ExcelpluginsfillPluginTable2ExcelpluginsformPluginTable2ExcelpluginsalignmentPluginTable2ExcelpluginshyperlinkPluginTable2ExcelpluginsautoWidthPlugin
A plugin can be defined to join different phase of table to excel process, and in different phase, plugin is able to access different objects from context.
/** * after an empty workbook created * @param * @param */ {} /** * after an empty worksheet created * @param * @param * @param * @param */ {} /** * after a worksheet been filled with data from table * @param * @param * @param * @param */ {} /** * after an cell of worksheet created * @param * @param * @param * @param * @param * @param * @param * @param */ {}
Example 1, you can define a plugin to make some rows or columns hidden of exported excel:
const table2Excel = 'table' plugins: { worksheethidden = true worksheethidden = true }
Example 2, you can add your customized cell parser for your table:
const table2Excel = 'table' plugins: { workcellvalue = text: '' link: '' workcellstyle = ...workcellstyle font: {} color: {} }