- Notifications
You must be signed in to change notification settings - Fork111
Javascript library to create "valid" excel file from html table with styles
License
linways/table-to-excel
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Export HTML table to valid excel file effortlessly.This library usesexceljs/exceljs under the hood to create the excel.
(Initial version of this library was usingprotobi/js-xlsx, it can be foundhere)
Just add a script tag:
<scripttype="text/javascript"src="../dist/tableToExcel.js"></script>
npm install @linways/table-to-excel --save
importTableToExcelfrom"@linways/table-to-excel";
Create your HTML table as normal.
To export content of table#table1 run:
TableToExcel.convert(document.getElementById("table1"));
or
TableToExcel.convert(document.getElementById("table1"),{name:"table1.xlsx",sheet:{name:"Sheet 1"}});
Checkthis pen for working example.
Cell types can be set using the following data attributes:
| Attribute | Description | Possible Values |
|---|---|---|
data-t | To specify the data type of a cell | s : String (Default)n : Numberb : Booleand : Date |
data-hyperlink | To add hyper link to cell | External URL or hyperlink to another sheet |
data-error | To add value of a cell as error |
Example:
<!-- for setting a cell type as number --><tddata-t="n">2500</td><!-- for setting a cell type as date --><tddata-t="d">05-23-2018</td><!-- for setting a cell type as boolean. String "true/false" will be accepted as Boolean--><tddata-t="b">true</td><!-- for setting a cell type as boolean using integer. 0 will be false and any non zero value will be true --><tddata-t="b">0</td><!-- For adding hyperlink --><tddata-hyperlink="https://google.com">Google</td>
All styles are set usingdata attributes ontd tags.There are 5 types of attributes:data-f-*,data-a-*,data-b-*,data-fill-* anddata-num-fmt which corresponds to five top-level attributesfont,alignment,border,fill andnumFmt.
| Category | Attribute | Description | Values |
|---|---|---|---|
| font | data-f-name | Font name | "Calibri" ,"Arial" etc. |
data-f-sz | Font size | "11" // font size in points | |
data-f-color | Font color | A hex ARGB value. Eg: FFFFOOOO for opaque red. | |
data-f-bold | Bold | true orfalse | |
data-f-italic | Italic | true orfalse | |
data-underline | Underline | true orfalse | |
data-f-strike | Strike | true orfalse | |
| Alignment | data-a-h | Horizontal alignment | left,center,right,fill,justify,centerContinuous,distributed |
data-a-v | Vertical alignment | bottom,middle,top,distributed,justify | |
data-a-wrap | Wrap text | true orfalse | |
data-a-indent | Indent | Integer | |
data-a-rtl | Text direction: Right to Left | true orfalse | |
data-a-text-rotation | Text rotation | 0 to 90 | |
| -1 to -90 | |||
| vertical | |||
| Border | data-b-a-s | Border style (all borders) | ReferBORDER_STYLES |
data-b-t-s | Border top style | ReferBORDER_STYLES | |
data-b-b-s | Border bottom style | ReferBORDER_STYLES | |
data-b-l-s | Border left style | ReferBORDER_STYLES | |
data-b-r-s | Border right style | ReferBORDER_STYLES | |
data-b-a-c | Border color (all borders) | A hex ARGB value. Eg: FFFFOOOO for opaque red. | |
data-b-t-c | Border top color | A hex ARGB value. | |
data-b-b-c | Border bottom color | A hex ARGB value. | |
data-b-l-c | Border left color | A hex ARGB value. | |
data-b-r-c | Border right color | A hex ARGB value. | |
| Fill | data-fill-color | Cell background color | A hex ARGB value. |
| numFmt | data-num-fmt | Number Format | "0" |
| "0.00%" | |||
| "0.0%" // string specifying a custom format | |||
| "0.00%;\(0.00%\);\-;@" // string specifying a custom format, escaping special characters |
BORDER_STYLES:thin,dotted,dashDot,hair,dashDotDot,slantDashDot,mediumDashed,mediumDashDotDot,mediumDashDot,medium,double,thick
To exclude a cell or a row from the exported excel adddata-exclude="true" to the correspondingtd ortr.
Example:
<!-- Exclude entire row --><trdata-exclude="true"><td>Excluded row</td><td>Something</td></tr><!-- Exclude a single cell --><tr><td>Included Cell</td><tddata-exclude="true">Excluded Cell</td><td>Included Cell</td></tr>
Column width's can be set by specifyingdata-cols-width in the<table> tag.data-cols-width accepts comma separated column widths specified in character count .data-cols-width="10,20" will set width of first coulmn as width of 10 charaters and second column as 20 characters wide.
Example:
<tabledata-cols-width="10,20,30"> ...</table>
Row Height can be set by specifyingdata-height in the<tr> tag.
Example:
<trdata-height="42.5"><td>Cell 1</td><td>Cell 2</td></tr>
Migration Guide for migrating from V0.2.1 to V1.0.0
- Changed the backend to Exceexceljs/exceljslJS
- Added border color
- Option to set style and color for all borders
- Exclude row
- Added text underline
- Added support for hyperlinks
- Text intent
- RTL support
- Extra alignment options
- String "true/false" will be accepted as Boolean
- Changed border style values
- Text rotation values changed
- Fixed bug in handling multiple columns merges in a sheet
- Option to specify row height
About
Javascript library to create "valid" excel file from html table with styles
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.