1 /**
2 * 功能:添加合计行
3 *
4 * @param table
5 * 指定的KDTable
6 * @param fields
7 * 需要合计的列
8 */
9 public static void apendFootRow(KDTable table, String fields[]) {
10 int size = fields.length;
11 if (size == 0)
12 return;
13 Map sumValue = new HashMap();
14 // 利用getRowCount的到的行可能不正确
15 int count = table.getRowCount3();
16
17 for (int i = 0; i < fields.length; i++) {
18 sumValue.put(fields[i], new BigDecimal("0.00"));
19 }
20 IRow footRow = null;
21 KDTFootManager footManager = table.getFootManager();
22 if (footManager == null) {
23 footManager = new KDTFootManager(table);
24 footManager.addFootView();
25 table.setFootManager(footManager);
26 }
27 // 计算所有指定行的合计值
28 footRow = footManager.getFootRow(0);
29 for (int i = 0; i < count; i++) {
30 IRow row = table.getRow(i);
31 for (int j = 0; j < fields.length; j++) {
32 sumValueForCell(row, fields[j], sumValue);
33 }
34 }
35
36 if (footRow == null) {
37 footRow = footManager.addFootRow(0);
38 }
39 // 设置合计行显示样式
40 String colFormat = "%{0.00}f";
41
42 String total = EASResource.getString(FrameWorkClientUtils.strResource
43 + "Msg_Total");
44
45 table.getIndexColumn().setWidthAdjustMode(KDTIndexColumn.WIDTH_MANUAL);
46 table.getIndexColumn().setWidth(30);
47 footManager.addIndexText(0, total);
48 footRow.getStyleAttributes().setBackground(new Color(0xf6, 0xf6, 0xbf));
49 for (int i = 0; i < size; i++) {
50 String colName = fields[i];
51 footRow.getCell(colName).getStyleAttributes().setNumberFormat(
52 colFormat);
53 footRow.getCell(colName).getStyleAttributes().setHorizontalAlign(
54 HorizontalAlignment.RIGHT);
55 footRow.getCell(colName).getStyleAttributes().setFontColor(
56 Color.black);
57 }
58
59 // 设置合计行的值
60 for (int i = 0; i < fields.length; i++) {
61 footRow.getCell(fields[i]).setValue(sumValue.get(fields[i]));
62 }
63 }
64
65 private static void sumValueForCell(IRow row, String key, Map sumValue) {
66 ICell cell = row.getCell(key);
67
68 if (cell != null) {
69 Object obj = cell.getValue();
70 if (obj != null) {
71 BigDecimal keyValue = (BigDecimal) sumValue.get(key);
72 keyValue = keyValue.add(new BigDecimal(obj.toString()));
73 sumValue.put(key, keyValue);
74 }
75 }
76 }