KDTable扩展功能
一、KDTable支持类似Excel公式的功能,当计算单元格出现错误时,如数据非法、被零除等错误信息如何进行设置?
详见下文:
1、定义信息提示解析类:
1 public class AnlysScriptErrorReport implements IScriptErrorHandler { 2 3 private String errorDiv0_DefaltValue = null; 4 5 public AnlysScriptErrorReport() { 6 } 7 8 public void setDiv0Value(String defaultValue) { 9 this.errorDiv0_DefaltValue = defaultValue; 10 } 11 12 public void handle(KDTable table, Exception exception, CellPosition cellPos, String scriptSource, int pos) { 13 if (pos == -1 || cellPos == null) { 14 exception.printStackTrace(); 15 } else { 16 if (table != null) { 17 /* 18 * 设置当公式是除且是除以0时显示的默认信息: 默认是显示:#DIV0 除数为零 19 * 可以通过该方法设置默认要显示的值:如可以设置为0,或- 20 */ 21 String errorMessage = exception.getMessage(); 22 if (exception instanceof SyntaxErrorException) { 23 SyntaxErrorException e = (SyntaxErrorException) exception; 24 long errorCode = e.getErrorCode(); 25 if (errorCode == ExprError.Div0) {// 如果是除数为零的Exception,错误信息特别处理 26 errorMessage = ""; 27 } else if (errorCode == ExprError.BadValue) { 28 errorMessage = ""; 29 } 30 } 31 table.getRow(cellPos.row).getCell(cellPos.col).getKDTCell().setValue(errorMessage); 32 } 33 } 34 } 35 }
2、应用信息解析类:
table.getScriptManager().setErrorHandler(new AnlysScriptErrorReport());
二、单元格为0的显示格式扩展
1、定义表格单元格内容显示器
1 public class AnlysFilterZeroParser implements IUserCellDisplayParser { 2 3 public Object parse(int rowIndex, int colIndex, ICell cell, Object value) { 4 if(value==null) return value; 5 String strValue=value.toString(); 6 7 if("0".equals(strValue) || "0.0".equals(strValue) || "0.00%".equals(strValue)) { 8 return ""; 9 } 10 return value; 11 } 12 }
2、应用单元格内容显示:
table.setUserCellDisplayParser(new AnlysFilterZeroParser());