jExcel
是一个轻量级的vanilla javascript插件,用于创建与Excel或任何其他电子表格软件兼容的基于Web的交互式表格和电子表格,可以创建可以交互的表格,兼容Excel,可以从 Js Array
、JSON
、CSV
、XSLX
文件创建表格。可以从Excel中直接复制,然后粘贴在jExcel
表格中。而且可以定制化,还可以结合第三方的库使用,支持 React
、Vue
、JQuery
等。
安装
- 通过
npm
安装
npm install jexcel
- 浏览器直接引用
<script src="https://bossanova.uk/jexcel/v4/jexcel.js"></script>
<script src="https://bossanova.uk/jsuites/v2/jsuites.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v2/jsuites.css" type="text/css" />
<link rel="stylesheet" href="https://bossanova.uk/jexcel/v4/jexcel.css" type="text/css" />
基本使用
需要先创建一个div
的容器,来显示表格
<div id="spreadsheet"></div>
需要在 script
中初始化表格,这样表格就会显示出来了
var data = [
['Jazz', 'Honda', '2019-02-12', '', true, '$ 2.000,00', '#777700'],
['Civic', 'Honda', '2018-07-11', '', true, '$ 4.000,01', '#007777'],
];
jexcel(document.getElementById('spreadsheet'), {
data:data,
columns: [
{ type: 'text', title:'Car', width:120 },
{ type: 'dropdown', title:'Make', width:200, source:[ "Alfa Romeo", "Audi", "Bmw" ] },
{ type: 'calendar', title:'Available', width:200 },
{ type: 'image', title:'Photo', width:120 },
{ type: 'checkbox', title:'Stock', width:80 },
{ type: 'numeric', title:'Price', width:100, mask:'$ #.##,00', decimal:',' },
{ type: 'color', width:100, render:'square', }
]
});
在React
中使用
class Jexcel extends React.Component {
constructor(props) {
super(props);
this.options = props.options;
this.wrapper = React.createRef();
}
componentDidMount = function() {
this.el = jexcel(this.wrapper.current, this.options);
}
addRow = function() {
this.el.insertRow();
}
render() {
return (
<div>
<div></div><br/><br/>
<input type='button' value='Add new row' onClick={() => this.addRow()}></input>
</div>
);
}
}
var options = {
data:[[]],
minDimensions:[10,10],
};
ReactDOM.render(<Jexcel options={options} />, document.getElementById('spreadsheet'))
在 Vue
中使用
import jexcel from 'jexcel'
import 'jexcel/dist/jexcel.css'
var data = [
['Jazz', 'Honda', '2019-02-12', '', true, '$ 2.000,00', '#777700'],
['Civic', 'Honda', '2018-07-11', '', true, '$ 4.000,01', '#007777']
]
var options = {
data: data,
allowToolbar:true,
columns: [
{ type: 'text', title: 'Car', width: '120px' },
{ type: 'dropdown', title: 'Make', width: '250px', source: [ 'Alfa Romeo', 'Audi', 'Bmw' ] },
{ type: 'calendar'