ZK Spreadsheet(表格)组件

壤驷穆冉
2023-12-01
zul页面代码
<?xml version="1.0" encoding="UTF-8"?>
<?taglib uri="http://www.zkoss.org/zss/function" prefix="ss" ?>
<zk>
 <window title="差旅费计算" use="org.zkoss.zss.demo.MainWindow"
  border="normal" style="heigth:300px;width:700px">
  <hbox>
   <label style="font-weight:bold" id="lbpos" value="A1"/>
   <textbox id="tbxval" width="600px" />
  </hbox>
  <spreadsheet id="ss1" url="/WEB-INF/travel.xls" maxrow="20" maxcolumn="20" height="300px" width="690px" style="border:1px solid #8AA3C1" />
  <vbox>
   <hbox>退还 (合计 * 0.5) : <label id="lb" value="${ss:formula(ss1.book, '=Sheet1!B5 * 0.5')}"/></hbox>
  </vbox>
  <zscript>
   ss1.book.addFormulaListener(new SimpleFormulaListener(lb, "value", "=Sheet1!B5 * 0.5"));
  </zscript>
 </window>
</zk>
java代码:MainWindow.java
package org.zkoss.zss.demo;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.ext.AfterCompose;
import org.zkoss.zss.engine.xel.Indexes;
import org.zkoss.zss.model.Book;
import org.zkoss.zss.model.Cell;
import org.zkoss.zss.model.Sheet;
import org.zkoss.zss.ui.CellEvent;
import org.zkoss.zss.ui.Events;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;


public class MainWindow extends Window implements AfterCompose{

	int lastRow=0;
	int lastCol=0;
	Book book;
	Spreadsheet spreadsheet;
	
	
	public void afterCompose() {
		spreadsheet = (Spreadsheet)getFellow("ss1");
		
		//设置要读取的xls文件
		book = spreadsheet.getBook();
		
		spreadsheet.addEventListener(Events.ON_CELL_FOUCSED,new EventListener(){
			public void onEvent(Event event) throws Exception {
				onCellEvent((CellEvent)event);
			}
		});
		spreadsheet.addEventListener(Events.ON_START_EDITING,new EventListener(){
			public void onEvent(Event event) throws Exception {
				onCellEvent((CellEvent)event);
			}
		});
		
		final Textbox tbxval = (Textbox)getFellow("tbxval");
		tbxval.addEventListener("onChange",new EventListener(){
			public void onEvent(Event event) throws Exception {
				doCellChange(tbxval.getValue());
			}
		});
	}
	void onCellEvent(CellEvent event){
		Sheet sheet = (Sheet)book.getSheets().get(event.getSheetIndex());
		lastRow = event.getRow();
		lastCol = event.getColumn();
		Label lbpos = (Label)getFellow("lbpos");
		Textbox tbxval = (Textbox)getFellow("tbxval");
		
		Cell cell = sheet.getCell(lastRow, lastCol);
		lbpos.setValue(Indexes.toA1(lastRow,lastCol,false,false));
		tbxval.setValue(cell == null ? "" : cell.getEditText());
	}
		
	void doCellChange(String value){
		if(lastRow == -1){
			return;
		}
		Sheet sheet = (Sheet)book.getSheets().get(0);
		Cell cell = sheet.getCell(lastRow, lastCol);
		if(cell==null){
			sheet.setCellValue(lastRow, lastCol, "");
			cell = (Cell)sheet.getCell(lastRow,lastCol);
		}
		cell.setEditText(value);
	}

}

 

使用说明:

1、双击单元格或者按F2键可以编辑单元格。

2、可以按上下左右键,HOME、END键来移动焦点。

3、编辑的时候,你可以输入文本,数字或者公式,比如=SUM(B1:B4)

4、按ENTER完成编辑,按ESC退出编辑

效果图:

 

 类似资料: