how to create web table like excel use jspreadsheet
时间: 2023-05-24 07:05:09 浏览: 155
To create a web table like Excel using jSpreadsheet, follow these steps:
1. Add the jspreadsheet script and CSS file to your HTML file.
2. Create a container element for the table in your HTML file.
3. In your script file, create a variable for the data you want to display in the table. This can be done in a variety of ways, such as hard-coding the data or fetching it from an API.
4. Initialize the jSpreadsheet plugin on the container element in your script file.
5. Configure the jSpreadsheet options to match your desired Excel-like behavior, such as allowing sorting and filtering, adding formulas, and formatting cells.
6. Populate the table with data by passing the data variable into the jSpreadsheet `setData` method.
7. (Optional) Add event listeners to the jSpreadsheet instance to handle user interactions with the table, such as clicking a cell or submitting a form.
Here's an example code snippet that demonstrates how to create a basic web table using jSpreadsheet:
```html
<!-- HTML -->
<div id="table-container"></div>
```
```javascript
// JavaScript
const data = [
['Name', 'Age', 'Gender'],
['John', '28', 'M'],
['Jane', '32', 'F'],
['Bob', '45', 'M'],
];
const container = document.getElementById('table-container');
const options = {
columnSorting: true,
columnDrag: true,
filters: true,
columnResize: true,
rowResize: true,
formulas: true,
groups: true,
mergeCells: true,
cellPopup: true,
contextMenu: true,
};
const jspreadsheet = $(container).jexcel(options);
jspreadsheet.setData(data);
```
This code will create a jSpreadsheet instance on the `#table-container` element, with the specified data and options. The resulting table will have Excel-like behavior, such as the ability to sort and filter columns, add formulas, and merge cells.
阅读全文
相关推荐

还不是很完善,可以自己加以封装
// JavaScript Document
var BoxWidth = 700// 资料表显示宽度 ( 不含卷轴 )
var ShowLine = 15// 资料表显示列数
var RsHeight = 21// 资料列高度
var LockCols = 1// 要锁定的栏位数 ( 由左至右 )
function WriteTable(){// 写入表格
var iBoxWidth=BoxWidth
var NewHTML=""
for(i=0;i<DataTitles.length;i++){
if(i<LockCols){
var cTitle=DataTitles[i].split("#")
iBoxWidth-=cTitle[1]
var DynTip=((i+1)==LockCols)?"解除锁定":"锁定此栏位"
NewHTML+="<td><span>"+cTitle[0]+""
}
}
NewHTML+=" "
for(i=0;i<DataTitles.length;i++){
if(i>=LockCols){
var cTitle=DataTitles[i].split("#")
NewHTML+=""+cTitle[0]+" "
}
}
NewHTML+=" "
DataTable.innerHTML=NewHTML
ApplyData()
}
function ApplyData(){// 写入资料
var NewHTML=""
for(i=0;i<DataFields.length;i++){
NewHTML+="<tr>"
for(j=0;j<DataTitles.length;j++){
if(j<LockCols){
var cTitle=DataTitles[j].split("#")
NewHTML+="<td><span>"+DataFields[i][j]+""
}
}
NewHTML+=""
}
NewHTML+=""
DataGroup1.innerHTML=NewHTML
var NewHTML=""
for(i=0;i<DataFields.length;i++){
NewHTML+="<tr>"
for(j=0;j<DataTitles.length;j++){
if(j>=LockCols){
var cTitle=DataTitles[j].split("#")
NewHTML+=""+DataFields[i][j]+" "
}
}
NewHTML+=""
}
NewHTML+=""
DataGroup2.innerHTML=NewHTML
DataFrame1.style.pixelHeight=RsHeight*ShowLine
DataFrame2.style.pixelHeight=RsHeight*ShowLine
DataFrame3.style.pixelHeight=RsHeight*ShowLine+RsHeight
DataGroup3.style.pixelHeight=RsHeight*(DataFields.length+1)
}
function ResetTable(n){
var iBoxWidth=0
for(i=0;i<DataTitles.length;i++){
if(i<(n+1)){
var cTitle=DataTitles[i].split("#")
iBoxWidth+=parseInt(cTitle[1])
}
}
if(iBoxWidth>BoxWidth){
var Sure=confirm("\n锁定栏位的宽度大於资料表显示的宽 \n\n度,这可能会造成版面显示不正常。\n\n\n您确定要继续吗?")
}else{
Sure=true
}
if(Sure){
LockCols=(LockCols==n+1)?0:n+1
WriteTable()
}
}
function SYNC_Roll(){
DataGroup1.style.posTop=-DataFrame3.scrollTop
DataGroup2.style.posTop=-DataFrame3.scrollTop
}
window.onload=WriteTable

















