Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Jul 25, 2021. It is now read-only.
/TableExportPublic archive

The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.

License

NotificationsYou must be signed in to change notification settings

clarketm/TableExport

Repository files navigation

TableExport

NPM releaseBuild StatusDownloadsLicense

Docs

Getting Started

Install manually using<script> tags

To use this library, include theFileSaver.js library, andTableExport library before the closing<body> tag of your HTML document:

<scriptsrc="FileSaver.js"></script>...<scriptsrc="tableexport.js"></script>

Install with Bower

$ bower install tableexport.js

Install with npm

$ npm install tableexport

CDN

uncompressedcompressed
CSS🔗🔗
JS🔗🔗
Images🔗xlsx🔗xls🔗csv🔗txt

Dependencies

Required:

Optional:

Add-Ons:

In order to provideOffice Open XML SpreadsheetML Format (.xlsx ) support, you must include the following third-party library in your project before bothFileSaver.js andTableExport.

Includingxlsx.core.js isNOT necessary if installing withBower ornpm

<scriptsrc="xlsx.core.js"></script><scriptsrc="FileSaver.js"></script>...<scriptsrc="tableexport.js"></script>

Older Browsers:

To support legacy browsers (Chrome < 20,Firefox < 13,Opera < 12.10,IE < 10,Safari < 6 ) include theBlob.js polyfill before theFileSaver.js script.

  • Blob.js byeligrey (forked byclarketm)

IncludingBlob.js isNOT necessary if installing withBower ornpm

<scriptsrc="Blob.js"></script><scriptsrc="FileSaver.js"></script>...<scriptsrc="tableexport.js"></script>

Usage

JavaScript

To use this library, simple call theTableExport constructor:

newTableExport(document.getElementsByTagName("table"));// OR simplyTableExport(document.getElementsByTagName("table"));// OR using jQuery$("table").tableExport();

Additional properties can be passed-in to customize the look and feel of your tables, buttons, and exported data.

Notice that by default, TableExport will create export buttons for three different filetypesxls,csv,txt. You can choose which buttons to generate by setting theformats property to the filetype(s) of your choice.

/* Defaults */TableExport(document.getElementsByTagName("table"),{headers:true,// (Boolean), display table headers (th or td elements) in the <thead>, (default: true)footers:true,// (Boolean), display table footers (th or td elements) in the <tfoot>, (default: false)formats:["xlsx","csv","txt"],// (String[]), filetype(s) for the export, (default: ['xlsx', 'csv', 'txt'])filename:"id",// (id, String), filename for the downloaded file, (default: 'id')bootstrap:false,// (Boolean), style buttons using bootstrap, (default: true)exportButtons:true,// (Boolean), automatically generate the built-in export buttons for each of the specified formats (default: true)position:"bottom",// (top, bottom), position of the caption element relative to table, (default: 'bottom')ignoreRows:null,// (Number, Number[]), row indices to exclude from the exported file(s) (default: null)ignoreCols:null,// (Number, Number[]), column indices to exclude from the exported file(s) (default: null)trimWhitespace:true,// (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: false)RTL:false,// (Boolean), set direction of the worksheet to right-to-left (default: false)sheetname:"id"// (id, String), sheet name for the exported spreadsheet, (default: 'id')});

Note: to use thexlsx filetype, you must includejs-xlsx; reference theAdd-Ons section.

Properties

Methods

TableExport supports additional methods (getExportData,update,reset andremove) to control theTableExport instance after creation.

/* First, call the `TableExport` constructor and save the return instance to a variable */vartable=TableExport(document.getElementById("export-buttons-table"));
/* get export data */varexportData=table.getExportData();// useful for creating custom export buttons, i.e. when (exportButtons: false)/***************** ** exportData *** *****************{    "export-buttons-table": {        xls: {            data: "...",            fileExtension: ".xls",            filename: "export-buttons-table",            mimeType: "application/vnd.ms-excel"        },        ...    }};*/
/* convert export data to a file for download */varexportData=table.getExportData();varxlsxData=exportData.table.xlsx;// Replace with the kind of file you want from the exportDatatable.export2file(xlsxData.data,xlsxData.mimeType,xlsxData.filename,xlsxData.fileExtension,xlsxData.merges,xlsxData.RTL,xlsxData.sheetname)
vartableId="export-buttons-table";varXLS=table.CONSTANTS.FORMAT.XLS;/* get export data (see `getExportData` above) */varexportDataXLS=table.getExportData()[tableId][XLS];/* get file size (bytes) */varbytesXLS=table.getFileSize(exportDataXLS.data,exportDataXLS.fileExtension);/********************************** ** bytesXLS (file size in bytes) **********************************352*/
/* update */table.update({filename:"newFile"// pass in a new set of properties});
/* reset */table.reset();// useful for a dynamically altered table
/* remove */table.remove();// removes caption and buttons

Settings

Below are some of the popular configurable settings to customize the functionality of the library.

/** * CSS selector or selector[] to exclude/remove cells (<td> or <th>) from the exported file(s). *@type {selector|selector[]} *@memberof TableExport.prototype */// selectorTableExport.prototype.ignoreCSS=".tableexport-ignore";// selector[]TableExport.prototype.ignoreCSS=[".tableexport-ignore",".other-ignore-class"];// OR using jQuery// selector$.fn.tableExport.ignoreCSS=".tableexport-ignore";// selector[]$.fn.tableExport.ignoreCSS=[".tableexport-ignore",".other-ignore-class"];
/** * CSS selector or selector[] to replace cells (<td> or <th>) with an empty string in the exported file(s). *@type {selector|selector[]} *@memberof TableExport.prototype */// selectorTableExport.prototype.emptyCSS=".tableexport-empty";// selector[]TableExport.prototype.emptyCSS=[".tableexport-empty",".other-empty-class"];// OR using jQuery// selector$.fn.tableExport.emptyCSS=".tableexport-empty";// selector[]$.fn.tableExport.emptyCSS=[".tableexport-empty",".other-empty-class"];
/* default charset encoding (UTF-8) */TableExport.prototype.charset="charset=utf-8";/* default `filename` property if "id" attribute is unset */TableExport.prototype.defaultFilename="myDownload";/* default class to style buttons when not using Bootstrap or the built-in export buttons, i.e. when (`bootstrap: false` & `exportButtons: true`)  */TableExport.prototype.defaultButton="button-default";/* Bootstrap classes used to style and position the export button, i.e. when (`bootstrap: true` & `exportButtons: true`) */TableExport.prototype.bootstrapConfig=["btn","btn-default","btn-toolbar"];/* row delimeter used in all filetypes */TableExport.prototype.rowDel="\r\n";
/** * Format-specific configuration (default class, content, mimeType, etc.) *@memberof TableExport.prototype */formatConfig:{/**     * XLSX (Open XML spreadsheet) file extension configuration     *@memberof TableExport.prototype     */xlsx:{defaultClass:'xlsx',buttonContent:'Export to xlsx',mimeType:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',fileExtension:'.xlsx'},xlsm:{defaultClass:'xlsm',buttonContent:'Export to xlsm',mimeType:'application/vnd.ms-excel.sheet.macroEnabled.main+xml',fileExtension:'.xlsm'},xlsb:{defaultClass:'xlsb',buttonContent:'Export to xlsb',mimeType:'application/vnd.ms-excel.sheet.binary.macroEnabled.main',fileExtension:'.xlsb'},/**     * XLS (Binary spreadsheet) file extension configuration     *@memberof TableExport.prototype     */xls:{defaultClass:'xls',buttonContent:'Export to xls',separator:'\t',mimeType:'application/vnd.ms-excel',fileExtension:'.xls',enforceStrictRFC4180:false},/**     * CSV (Comma Separated Values) file extension configuration     *@memberof TableExport.prototype     */csv:{defaultClass:'csv',buttonContent:'Export to csv',separator:',',mimeType:'text/csv',fileExtension:'.csv',enforceStrictRFC4180:true},/**     * TXT (Plain Text) file extension configuration     *@memberof TableExport.prototype     */txt:{defaultClass:'txt',buttonContent:'Export to txt',separator:'  ',mimeType:'text/plain',fileExtension:'.txt',enforceStrictRFC4180:true}},//////////////////////////////////////////// Configuration override example///////////////////////////////////////////* Change the CSV (Comma Separated Values) `mimeType` to "application/csv" */TableExport.prototype.formatConfig.xlsx.mimeType="application/csv"

CSS

TableExport packages with customizedBootstrap CSS stylesheets to deliver enhanced table and button styling. These styles can beenabled by initializing with thebootstrap property set totrue.

TableExport(document.getElementsByTagName("table"),{bootstrap:true});

When used alongside Bootstrap, there are four custom classes.xlsx,.xls,.csv,.txt providing button styling for each of the exportable filetypes.

Browser Support

ChromeFirefoxIEOperaSafari
Android--
iOS---
Mac OSX-
Windows

A full list ofbrowser support can be found in theFileSaver.js README. Somelegacy browsers may require an additional third-party dependency:Blob.js

Examples

Customizing Properties

Customizing Settings

Miscellaneous

Skeletons

License

TableExport is licensed under the terms of theApache-2.0 License

Going Forward

TODOs

  • Update JSDocs and TypScript definition file.
  • Fix bug withCSV andTXTignoreRows andignoreCols (rows/cols rendered as empty strings rather than beingremoved).
  • Reimplement and test theupdate,reset, andremoveTableExport prototype properties without requiring jQuery.
  • Make jQuery aspeer dependency and ensure properTableExport rendering in browser, AMD, and CommonJS environments.
  • Force jQuery to be an optionally loaded module.
  • Use the enhancedSheetJSxls,csv, andtxt formats (exposed viaenforceStrictRFC4180 prototype property).
  • AllowignoreCSS andemptyCSS to work with anyselector|selector[] instead of solely a single CSS class.
  • Ensure (via testing) full consistency and backwards-compatibility for jQuery.
  • AddExport as PDF support.

Credits

Special thanks the the following contributors:


[8]ページ先頭

©2009-2025 Movatter.jp