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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| package easyexceldemo.dto;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.merge.AbstractMergeStrategy; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import easyexceldemo.entity.User; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class BizMergeStrategy extends AbstractMergeStrategy {
private Map<String, List<RowRangeDto>> strategyMap; private Sheet sheet;
public BizMergeStrategy(Map<String, List<RowRangeDto>> strategyMap) { this.strategyMap = strategyMap; }
@Override protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) { this.sheet = sheet; if (cell.getRowIndex() == 2 && cell.getColumnIndex() == 0) {
for (Map.Entry<String, List<RowRangeDto>> entry : strategyMap.entrySet()) { Integer columnIndex = Integer.valueOf(entry.getKey()); entry.getValue().forEach(rowRange -> { sheet.addMergedRegionUnsafe(new CellRangeAddress(rowRange.getStart(), rowRange.getEnd(), columnIndex, columnIndex)); }); } } }
public static Map<String, List<RowRangeDto>> addAnnualMerStrategy(List<User> projectDtoList) { Map<String, List<RowRangeDto>> strategyMap = new HashMap<>(); User preUser = null; for (int i = 0; i < projectDtoList.size(); i++) { User curUser = projectDtoList.get(i); if (preUser != null) { if (curUser.getName() == preUser.getName()){
BizMergeStrategy.fillStrategyMap(strategyMap, "1", i+1); } } preUser = curUser; } return strategyMap; }
private static void fillStrategyMap(Map<String, List<RowRangeDto>> strategyMap, String key, int index){ List<RowRangeDto> rowRangeDtoList = strategyMap.get(key) == null ? new ArrayList<>() : strategyMap.get(key); boolean flag = false; for (RowRangeDto dto : rowRangeDtoList) { if (dto.getEnd() == index) { dto.setEnd(index + 1); flag = true; } } if (!flag) { rowRangeDtoList.add(new RowRangeDto(index, index + 1)); } strategyMap.put(key, rowRangeDtoList); }
public static HorizontalCellStyleStrategy CellStyleStrategy(){ WriteCellStyle headWriteCellStyle = new WriteCellStyle(); headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontHeightInPoints((short)13); headWriteFont.setBold(true); headWriteCellStyle.setWriteFont(headWriteFont); headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); return horizontalCellStyleStrategy; } }
|