Giter VIP home page Giter VIP logo

table2excel.js's Introduction

Table2Excel.js

This is a library to export html tables to excel sheets.

Precondition

It has to be a row * column table

Features

  1. able to export with width, alignment and colors
  2. extendable

Dependencies

ExcelJS

FileSaver.js

Live Demo

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:

import Table2Excel from 'table2excel.js'
new Table2Excel('table').export()

you may also need a config in webpack:

node: { fs: 'empty' }

table2excel.min.js (with ExcelJS packed)

<script src="path/to/table2excel.min.js"></script>

table2excel.core.js (without ExcelJS packed)

<script src="path/to/exceljs.min.js"></script>
<script src="path/to/table2excel.core.js"></script>

Basic Usage

const table2Excel = new Table2Excel(selector, options)  // new Table2Excel('table')
table2Excel.export(fileName, extension) // 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: [
    Table2Excel.plugins.fontPlugin,
    Table2Excel.plugins.fillPlugin,
    Table2Excel.plugins.formPlugin,
    Table2Excel.plugins.alignmentPlugin,
    Table2Excel.plugins.hyperlinkPlugin,
    Table2Excel.plugins.autoWidthPlugin
  ]
}

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:

Table2Excel.plugins.fontPlugin,
Table2Excel.plugins.fillPlugin,
Table2Excel.plugins.formPlugin,
Table2Excel.plugins.alignmentPlugin,
Table2Excel.plugins.hyperlinkPlugin,
Table2Excel.plugins.autoWidthPlugin

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  {ExcelJS.Workbook} context.workbook
   * @param  {NodeList} context.tables   
   */
  workbookCreated ({ workbook, tables }) {},
  /**
   * after an empty worksheet created
   * @param  {ExcelJS.Workbook} workbook
   * @param  {NodeList} tables
   * @param  {ExcelJS.Worksheet} worksheet
   * @param  {HTMLTableElement} table
   */
  worksheetCreated ({ workbook, tables, worksheet, table }) {},
  /**
   * after a worksheet been filled with data from table
   * @param  {ExcelJS.Workbook} workbook
   * @param  {NodeList} tables
   * @param  {ExcelJS.Worksheet} worksheet
   * @param  {HTMLTableElement} table
   */
  worksheetCompleted ({ workbook, tables, worksheet, table }) {},
  /**
   * after an cell of worksheet created
   * @param  {ExcelJS.Workbook} workbook
   * @param  {NodeList} tables
   * @param  {ExcelJS.Worksheet} worksheet
   * @param  {HTMLTableElement} table
   * @param  {ExcelJS.Cell} workcell
   * @param  {HTMLTableCellElement} cell
   * @param  {colRange} [from, to]
   * @param  {rowRange} [from, to]
   */
  workcellCreated ({ workbook, tables, worksheet, table, workcell, cell, cellStyle, colRange, rowRange }) {}
}

Example 1, you can define a plugin to make some rows or columns hidden of exported excel:

const table2Excel = new Table2Excel('table', {
  plugins: [{
    worksheetCompleted ({ workbook, tables, worksheet, table }) {
      worksheet.getRow(1).hidden = true
      worksheet.getColumn(1).hidden = true
    }
  }]
})

Example 2, you can add your customized cell parser for your table:

const table2Excel = new Table2Excel('table', {
  plugins: [{
    workcellCreated ({ workbook, tables, worksheet, table, workcell, cell, cellStyle, rowRange, colRange }) {
      workcell.value = { text: '', link: '' }
      workcell.style = {
        ...workcell.style,
        font: {},
        color: {}
      }
    }
  }]
})

table2excel.js's People

Contributors

daydayhappychao avatar jackgit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

table2excel.js's Issues

无法合并单元格

无法合并单元格,“显示部分内容存在问题。是否让我们尽量修复”,控制台没有看到任何问题,但是下载demo页面的没有问题,下载demo页面的js也是无法合并单元格,完全不懂发生了什么

Cannot read property 'el' of undefined

Cannot read property 'el' of undefined
at Table2Excel.toSheet (table2excel.js?3378:161)
at eval (table2excel.js?3378:104)
at Array.forEach ()
at Table2Excel.toExcel (table2excel.js?3378:98)
at Table2Excel._export (table2excel.js?3378:199)
at VueComponent.exportTest (StatisticsView.vue?e8d4:313)
at VueComponent.invoker (vue.esm.js?65d7:2027)
at VueComponent.Vue.$emit (vue.esm.js?65d7:2538)
at VueComponent.handleClick (iview.js?5654:10403)
at invoker (vue.esm.js?65d7:2027)

table code:
<table border="1" class="test"> <thead> <tr style="text-align: right;"> <th></th> <th></th> <th>过货票数(排重)</th> <th>过货次数</th> <th>开包率</th> <th>过货件数(实际)</th> <th>过货时长</th> <th>图片数</th> <th>开包次数</th> <th>测试</th> </tr> <tr> <th>通道</th> <th>货物类型名</th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th>C仓通道1</th> <th>超大超重货物</th> <td>1</td> <td>1</td> <td>0%</td> <td>23</td> <td>15.0</td> <td>0.0</td> <td>0.0</td> <td>test</td> </tr> <tr> <th rowspan="4" valign="top">C仓通道3</th> <th>危险品货物</th> <td>4</td> <td>5</td> <td>22%</td> <td>12</td> <td>62.0</td> <td>0.0</td> <td>22.0</td> <td>test</td> </tr> <tr> <th>快件货物</th> <td>2</td> <td>2</td> <td>0.32%</td> <td>2</td> <td>0.0</td> <td>19.0</td> <td>6.0</td> <td>test</td> </tr> <tr> <th>超大超重货物</th> <td>2</td> <td>2</td> <td>1.78%</td> <td>17</td> <td>3.0</td> <td>9.0</td> <td>16.0</td> <td>test</td> </tr> <tr> <th>邮件货物</th> <td>7</td> <td>11</td> <td>30%</td> <td>356</td> <td>5618.0</td> <td>0.0</td> <td>30.0</td> <td>test</td> </tr> <tr> <th rowspan="5" valign="top">测试通道1</th> <th>危险品货物</th> <td>13</td> <td>22</td> <td>0.03%</td> <td>22222220000</td> <td>1483.0</td> <td>1353.0</td> <td>44.0</td> <td>test</td> </tr> <tr> <th>快件货物</th> <td>5</td> <td>17</td> <td>0.01%</td> <td>194</td> <td>2997.0</td> <td>2129.0</td> <td>28.0</td> <td>test</td> </tr> <tr> <th>普通货物</th> <td>21</td> <td>46</td> <td>0.02%</td> <td>1013</td> <td>539.0</td> <td>3941.0</td> <td>78.0</td> <td>test</td> </tr> <tr> <th>超大超重货物</th> <td>10</td> <td>34</td> <td>0.03%</td> <td>175</td> <td>191.0</td> <td>1485.0</td> <td>44.0</td> <td>test</td> </tr> <tr> <th>邮件货物</th> <td>29</td> <td>70</td> <td>0.03%</td> <td>2925</td> <td>4926.0</td> <td>4867.0</td> <td>147.0</td> <td>test</td> </tr> <tr> <th>快件货物</th> <td>3</td> <td>9</td> <td>27%</td> <td>58</td> <td>210.0</td> <td>1.0</td> <td>27.0</td> <td>test</td> </tr> <tr> <th>普通货物</th> <td>6</td> <td>18</td> <td>0.67%</td> <td>126</td> <td>1166.0</td> <td>6.0</td> <td>4.0</td> <td>test</td> </tr> <tr> <th>超大超重货物</th> <td>4</td> <td>15</td> <td>0%</td> <td>59</td> <td>868.0</td> <td>78.0</td> <td>0.0</td> <td>test</td> </tr> <tr> <th>邮件货物</th> <td>10</td> <td>47</td> <td>0.01%</td> <td>3480</td> <td>207.0</td> <td>1413.0</td> <td>14.0</td> <td>test</td> </tr> <tr> <th>超大超重A区</th> <th>邮件货物</th> <td>1</td> <td>1</td> <td>0%</td> <td>0</td> <td>15.0</td> <td>0.0</td> <td>0.0</td> <td>test</td> </tr> <tr> <th>All</th> <th></th> <td>120</td> <td>302</td> <td>82%</td> <td>22222230000</td> <td>18300.0</td> <td>15311.0</td> <td>461.0</td> <td>test</td> </tr> </tbody> </table>

Error with Angular 8

Hello!
I tried to update Angular version to 8 and encountered an error:
ERROR in ./node_modules/exceljs/dist/es5/csv/csv.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\exceljs\dist\es5\csv' ERROR in ./node_modules/exceljs/dist/es5/utils/utils.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\exceljs\dist\es5\utils' ERROR in ./node_modules/exceljs/dist/es5/xlsx/xlsx.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\exceljs\dist\es5\xlsx' ERROR in ./node_modules/fast-csv/lib/index.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib' ERROR in ./node_modules/fast-csv/lib/formatter/index.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/formatter/formatter_stream.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/formatter/formatter.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/parser/index.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\parser' ERROR in ./node_modules/fast-csv/lib/parser/parser_stream.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\parser' ERROR in ./node_modules/fast-csv/lib/extended.js Module not found: Error: Can't resolve 'os' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib' ERROR in ./node_modules/exceljs/dist/es5/utils/stream-buf.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\exceljs\dist\es5\utils' ERROR in ./node_modules/fast-csv/lib/formatter/index.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/formatter/formatter_stream.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/formatter/formatter.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\formatter' ERROR in ./node_modules/fast-csv/lib/parser/index.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\parser' ERROR in ./node_modules/fast-csv/lib/parser/parser_stream.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\fast-csv\lib\parser' ERROR in ./node_modules/jszip/lib/readable-stream-browser.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ikretinin\Documents\Lm.Vacuum\GMCS.LM.Vacuum.Web\ClientApp\node_modules\jszip\lib'

How can I fix it?

don't support colspan or rowspan

<table id="tb" >
          <tr>
            <td colspan="2">1</td>
            <td rowspan="2"> 2</td>
            <td>3</td>
          </tr>
          <tr>
            <td>a</td>
            <td>b</td>
            <td>c</td>
          </tr>
        </table>

it report an error

Uncaught TypeError: Cannot read property '0' of null
    at Object.decodeAddress (table2excel.min.js:7)
    at e.exports._set_tl_br (table2excel.min.js:7)
    at e.exports.decode (table2excel.min.js:7)
    at e.exports.decode (table2excel.min.js:7)
    at new e.exports (table2excel.min.js:7)
    at e.exports.mergeCells (table2excel.min.js:14)
    at d (table2excel.min.js:7)
    at table2excel.min.js:7
    at Array.forEach (<anonymous>)
    at e.value (table2excel.min.js:7)

Cannot read property 'el' of undefined

Cannot read property 'el' of undefined
at e.value (table2excel.min.js:8)

Here is something I find in your code in table2excel.min.js.
p = _.el: This _.el is undefined.
for (var c = 0, f = 0; f < n; f++) for (var m = 0; m < i; m++) if (!u[f][m].cell) { var _ = o[c++], p = _.el, y = p.rowSpan, g = p.colSpan; _.rowRange = {from: f, to: f}, _.colRange = {from: m, to: m}; for (var v = f; v < f + y; v++) for (var M = m; M < m + g; M++) u[v][M].cell = _, _.colRange.to = M, _.rowRange.to = v }

My own code is involved with "rowSpan" and "colSpan".Hence this error occurs.

导出的文件无法打开

我使用的环境是 webpack4+vue.js, 导出代码如下:
new Table2Excel("zd_table_trail").export("查询结果");
zd_table_trail是页面上表格的名字, 文件导出后无法打开,
表格数据如下:
站点名称 | 记录时间
未分类站点 | 2018-09-05 21:50:07
我看您的代码可以导出纯数据, 所以打算用你的, 但是试了下, 无论是页面表格还是纯数据, 导出后都无法打开, 我在 office 和wps都试过, 均打不开!

table中td不满的话,会报错

标题1 标题2 标题3
1 2 3
11 22
类似这种的,会报错,修改table2excel.js 中 toSheet方法
const cell = cells[cursor++]
之后加入对cell判断是否null,就可以修复了

隐藏内容不导出

部分table中包含隐藏行,默认导出为全部能容,能否提供接口仅完成非隐藏内容导出

如何导出表格中的图片

请问在导出多个工作簿的同时如何将表格中的图片一起导出,希望能够图片大小与表格大小相适应

Can I style borders ?

If I apply a border in CSS to my table cells, it is not exported in the Excel files. It is working with background-color for exemple, but not border.

Is it possible to add borders?

Thanks

how to work in intenet explorer

i like this project, but must use with internet explorer.
on load sample have error : "object does not support ... 'values'" (table2excel.core.js (1,3758))

there is any way to solve ?
thanks for any advance

Can't make it work

Tried to export a table with a class selector like
const table2Excel = new Table2Excel('.myclass');
table2Excel.export() exports an empty spreadsheet

导出的Excel太大时间过长会导致浏览器假死

如果导出的excel数据量太大会导致浏览器假死,希望能加入web worker,将导出的操作放在worker中。这样也方便通过内置的接口来监听导出的进度,加快主线程中进度条的渲染速度,希望作者考虑一下。

你好,字体加粗变大,表格加边框这些要怎么写?没看到说明

$(document).ready(function () {
$("#btnExport").click(function () {
new Table2Excel('#test', {
plugins: [{
workcellCreated ({ workbook, tables, worksheet, table, workcell, cell, cellStyle, rowRange, colRange }) {
workcell.style = {
...workcell.style,
font: {“这里面怎么写,想操作单独的行或列呢”},
color: {“这里面怎么写,想操作单独的行或列呢”}
}
}
}]
}).export('日志', 'xls');
});
});

Bold not working

Tried exporting a html table with style property font-weight:bold,but table exported without bold

new Table2Excel('table', { plugins: [Table2Excel.plugins.fontPlugin] }).export();

Sources

Hi,

I tried to fork your repository and faced problems after build. When I use your dist, font colors and sizes in cells are the same as in an html table but with my dist I see default styles.

How do you build and are all changes pushed in repo?

Thanks,
Danila Alekseev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.