KDTable 表达式应用工具类

来源:互联网 发布:女装淘宝货源 编辑:程序博客网 时间:2024/05/21 14:43
近期在EAS开发过程中,由于表格内有很多需要联动处理的逻辑,比如,价格单元格变化后,需要联动计算金额,明细修改后需要联动处理合计值等,通常的处理是添加表格编辑事件,处理以上业务逻辑,经过研究发现KDTable是支持类似excel公式的,以下是开发过程中提供的工具方法,用于设置一些常用的公式,如:sum、加法、减法、IF等。由于公式中使用的单元格索引,类似excel中的列头表示方式如:A1=sum(B1:B5),如何根据单元格索引获取对应的列头,着实耗费了一些时间。
  1   /**  2      * 给单元格添加sum表达式  3      * @param cell  4      * @param from  5      * @param to  6      */  7     public static void setCellSumExpr(ICell cell, int from, int to) {  8         cell.setExpressions(getSumExpr(from, to));  9     } 10      11     /** 12      * 给单元格添加IF表达式 13      * @param cell 14      * @param condExpr 15      * @param expr1 16      * @param expr2 17      */ 18     public static void setCellIFExpr(ICell cell,String condExpr,String expr1,String expr2){ 19         cell.setExpressions(getIFExpr(condExpr, expr1, expr2)); 20     } 21      22     /** 23      * 给单元格添加Add表达式 24      * @param cell 25      * @param a 26      * @param b 27      */ 28     public static void setCellAddExpr(ICell cell,int a,int b){ 29         cell.setExpressions(getAddExpr(a, b)); 30     } 31      32     /** 33      * 给单元格添加Add表达式,允许不连续区域求和 34      * @param cell 35      * @param range 36      */ 37     public static void setCellAddRangeExpr(ICell cell,int[] range){ 38         cell.setExpressions(getAddRangeExpr(range)); 39     } 40      41     /** 42      * 给单元格添加Substract表达式 43      * @param cell 44      * @param a 45      * @param b 46      */ 47     public static void setCellSubExpr(ICell cell,int a,int b){ 48         cell.setExpressions(getSubExpr(a, b)); 49     } 50  51     // return =sum(from:to); 52     public static String getSumExpr(int from, int to) { 53         StringBuffer buff = new StringBuffer(); 54         buff.append("=SUM(").append(getExcelColumnLabel(from)); 55         buff.append(":").append(getExcelColumnLabel(to)); 56         buff.append(")"); 57         return buff.toString().intern(); 58     } 59  60     // return =a+b 61     public static String getAddExpr(int a, int b) { 62         StringBuffer buff = new StringBuffer(); 63         buff.append("=").append(getExcelColumnLabel(a)); 64         buff.append("+").append(getExcelColumnLabel(b)); 65         return buff.toString().intern(); 66     } 67  68     // return =range[0]+range[1]+...+range[n] 69     public static String getAddRangeExpr(int[] range) { 70         StringBuffer buff = new StringBuffer(); 71         buff.append("="); 72  73         boolean flag = false; 74         for (int i = 0, n = range.length; i < n; i++) { 75             if (flag) { 76                 buff.append("+"); 77             } 78             buff.append(getExcelColumnLabel(range[i])); 79             flag = true; 80         } 81         return buff.toString().intern(); 82     } 83  84     // return =a-b 85     public static String getSubExpr(int a, int b) { 86         StringBuffer buff = new StringBuffer(); 87         buff.append("=").append(getExcelColumnLabel(a)); 88         buff.append("-").append(getExcelColumnLabel(b)); 89         return buff.toString().intern(); 90     } 91  92     // retur =IF(condExpr,expr1,expr2); 93     public static String getIFExpr(String condExpr, String expr1, String expr2) { 94         StringBuffer buff = new StringBuffer(); 95         buff.append("=IF(").append(condExpr).append(","); 96         buff.append(expr1).append(",").append(expr2); 97         buff.append(")"); 98         return buff.toString(); 99     }100 101     /** 根据列的的位置获取列标,如A、AA、AB...采用类似进制转换的算法 */102     public static String getExcelColumnLabel(int colCount) {103         String rs = "";104         do {105             colCount--;106             rs = ((char) (colCount % 26 + (int) 'A')) + rs;107             colCount = (int) ((colCount - colCount % 26) / 26);108         } while (colCount > 0);109         return rs;110     }111 112     /** 根据列标获取列的索引,采用类似进制转换的算法 */113     public static int getExcelColumnIndex(String colName) {114         if (colName == null || colName.equals("")) {115             return -1;116         }117         colName = colName.toUpperCase();118 119         // 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。120         int count = -1;121         char[] cs = colName.toCharArray();122         for (int i = 0; i < cs.length; i++) {123             count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);124         }125         return count;126     }