1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
public String generateTable(String tableStyle, Map<Integer,String> columnStyles, Map<Integer, Map<String, String>> cellStyles, String... mergerColFields) { Map<Integer, Map<String, Integer>> mergeCols = new HashMap<>(); Map<String, Integer> startRowMap = new HashMap<>(); Map<String, Integer> rowsCountMap = new HashMap<>(); for(String mergerColField:mergerColFields) { rowsCountMap.put(mergerColField, 0); startRowMap.put(mergerColField, 0); } Map<String, Object> lastValues = new HashMap<>(); int row = 0; for(Object element : collection) { for(String mergerColField:mergerColFields) { Object fieldValue = getFieldValueByName(element, mergerColField); if(row==0) { lastValues.put(mergerColField, fieldValue); } Object value = lastValues.get(mergerColField); if((value!=null && value.equals(fieldValue)) || (value==null && fieldValue==null)) { rowsCountMap.put(mergerColField, rowsCountMap.get(mergerColField) + 1); } else if(rowsCountMap.get(mergerColField) >1) { Map<String, Integer> fieldCols = mergeCols.get(row) == null?new HashMap<>():mergeCols.get(row); fieldCols.put(mergerColField, rowsCountMap.get(mergerColField)); mergeCols.put(startRowMap.get(mergerColField), fieldCols); rowsCountMap.put(mergerColField, 1); startRowMap.put(mergerColField, row); } else { startRowMap.put(mergerColField, row); } lastValues.put(mergerColField, fieldValue); if(row == collection.size()-1 && rowsCountMap.get(mergerColField)>1) { int startRow = startRowMap.get(mergerColField); Map<String, Integer> fieldCols = mergeCols.get(startRow) == null?new HashMap<>():mergeCols.get(startRow); fieldCols.put(mergerColField, rowsCountMap.get(mergerColField)); mergeCols.put(startRow, fieldCols); } } row++; } StringBuilder content = new StringBuilder(); if(tableStyle == null) { content.append(DEFAULT_TABLE_STYLE); } else { content.append(tableStyle); }
content.append("<tr>"); for(String key:this.keySet()) { content.append("<td><h4>").append(get(key)).append("</h4>"); } content.append("</tr>"); row = 0; rowsCountMap.clear(); for(Object element : collection) { if(columnStyles!=null && columnStyles.get(row) != null) { content.append("<tr style\"").append(columnStyles.get(row)).append("\">"); } else { content.append("<tr>"); } Map<String, Integer> fieldCols = mergeCols.get(row); rowsCountMap.forEach((k,v) -> { rowsCountMap.put(k, v-1); }); for(String key:this.keySet()) { if(rowsCountMap.get(key)!=null && rowsCountMap.get(key) > 0) continue; content.append("<td "); if(fieldCols!=null && fieldCols.get(key)!=null) { rowsCountMap.put(key, fieldCols.get(key)); content.append("rowspan=\"").append(fieldCols.get(key)).append("\""); } if(cellStyles!=null && cellStyles.get(0)!=null && cellStyles.get(0).get(key) != null) { content.append("style=\"").append(cellStyles.get(0).get(key)).append("\""); } content.append(">"); Object value = getFieldValueByName(element, key); if(value != null) { if(value instanceof java.math.BigDecimal) { if(bigDecimalFormat == HtmlGeneratorEnum.BIGDECIMAL_PERCENTAGE_FORMAT) { value = ((BigDecimal) value).multiply(BigDecimal.valueOf(100)).setScale(2); } content.append(value).append("%"); } else { content.append(value); } } content.append("</td>"); } content.append("</tr>"); row++; } content.append("</table>");
return content.toString(); }
|