Giter Site home page Giter Site logo

rusty1s / table2excel Goto Github PK

View Code? Open in Web Editor NEW
112.0 9.0 113.0 636 KB

Convert and download html tables to a xlsx-file that can be opened in Microsoft Excel

Home Page: http://rusty1s.github.io/table2excel/demo

License: MIT License

HTML 8.82% CSS 0.45% JavaScript 90.73%

table2excel's Introduction

table2excel

table2excel is a ecma5 compiled vanilla javascript plugin to convert and download html tables to a xlsx-file that can be opened in Microsoft Excel.

It uses the awesome js-xlsx plugin from Sheet JS as a dependency. Thanks!

Demo

Quick Start

<script src="table2excel.js"></script>

<script>
  var table2excel = new Table2Excel();
  table2excel.export(document.querySelectorAll("table"));
</script>

Node

// npm install table2excel --save
import 'table2excel';
const Table2Excel = window.Table2Excel;

const table2excel = new Table2Excel(options);

See my webpack configuration to see how to get js-xlsx running with webpack in the browser.

Additional options

You can pass in options as a parameter like new Table2Excel(options). The currently supported options are:

  • defaultFileName: The general file name of a downloaded document. Default: 'file'. Can also be adjusted individually for export as a second parameter, e.g. table2excel.export(table, "cool table");.
  • tableNameDataAttribute: Data attribute name to identify the worksheet name of a table. Default: 'excel-name'. Usage: <table data-excel-name="Check this out">...</table>. If not set, worksheets are numbered incrementally from 1.

Cell type handlers

table2excel detects a few special cells by default to display them correctly in Excel:

  • Numbers
  • Inputs: for input[type="text"], select or textarea elements
  • Dates: tries to parse the date or set the exact timestamp via data-timestamp on the cell (recommended!)
  • Booleans: parses 'true', 'false' or checkboxes/radios without text to booleans
  • Lists: parses <ul>...</ul> or <ol>...</ol> list elements to '..., ...'

Everything else will just get displayed as simple text. You can easily though add custom type handlers for your own needs:

Table2Excel.extend((cell, cellText) => {
  // {HTMLTableCellElement} cell - The current cell.
  // {string} cellText - The inner text of the current cell.

  // cell should be described by this type handler
  if (selector) return {
    t: ...,
    v: ...,
  };

  // skip and run next handler
  return null;
});

The return value must be a js-xlsx cell object.

Contributing

If you would like to submit a pull request with any changes you make, please feel free! Simply run npm test to test and npm start to compile before submitting pull requests.

Issues

Please use the GitHub issue tracker to raise any problems or feature requests.

table2excel's People

Contributors

igorlesnevskiy avatar rusty1s 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  avatar

table2excel's Issues

Adding formula to cell not working

Hi i succces download my html table to excel but problem is that script parse number by self. So i find that we can set the cell type and value. Also this i did sucessful bit then i want to add formula to cell and i dont get it to work. So what i missed up?

My code for now is:
Table2Excel.extend((cell, cellText) => { if (cellText == 'URE') { return { f: 'A1+A2' }; } else { return { t: 's', v: cellText } } });

Problem is that this cell is empty.

String Format Issue

I want to export this date "MNG 2022-001". However when I export it will show a date format. Could you fix it?

Display on browser:
image

Exported file:
image

Table pulled does not have the style

Hi,
First of all this is very helpful, Thanks.
Second, When I export my tables from html to excel, the styling is not there in excel.
And in one case, my whole table got hidden. Like in excel it was starting from J Column. I dont know why it did not start from A column.

ๆŠฅ้”™

Uncaught TypeError: Cannot read property 'length' of undefined
at Class.render (table.js:234)
at new Class (table.js:145)
at Object.table.render (table.js:1852)
at bindEvent (:331:7)
at Layui. (:345:5)
at Layui. (cover.js:836)
at onCallback (layui.js:175)
at Layui.use (layui.js:184)
at onCallback (layui.js:175)
at Layui.use (layui.js:184)

Format Number in Cell

I have a problem when exporting numbers, with separator format of thousands (,) ejem: 1,234.05 --- DO NOT take it as number, in excel, another problem is when I have numbers like 04.10 ... it takes as date, and I do not like Number ... any recommendations?

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.