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
130
131
132
133
134
135
136
137
138
139
140
141
package com.dy.pipIrrGlobal.excel;
 
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.springframework.util.PropertyPlaceholderHelper;
 
import java.util.List;
import java.util.Objects;
import java.util.Properties;
 
/**
 * @author ZhuBaoMin
 * @date 2024-10-12 11:40
 * @LastEditTime 2024-10-12 11:40
 * @Description 总账表格设置
 */
public class CellWriteHandler extends AbstractVerticalCellStyleStrategy {
    private String title;
 
    PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");
 
    public CellWriteHandler(String title) {
        this.title = title;
    }
 
    // 动态导入标题
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
        if (head != null) {
            List<String> headNameList = head.getHeadNameList();
            if (CollectionUtils.isNotEmpty(headNameList)) {
                Properties properties = new Properties();
                properties.setProperty("title", title);
                headNameList.replaceAll(value -> propertyPlaceholderHelper.replacePlaceholders(value, properties));
            }
        }
    }
 
    // 设置标题样式,包含字体、背景
    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {
        // 获取和创建CellStyle
        WriteCellData<?> cellData = context.getFirstCellData();
        CellStyle originCellStyle = cellData.getOriginCellStyle();
        Cell cell = context.getCell();
 
        if (Objects.isNull(originCellStyle)) {
            originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
        }
 
        ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255), new DefaultIndexedColorMap()));
 
        // 设置指定列背景色
        //if(cell.getColumnIndex() == 10
        //) {
        //    ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap()));
        //}
 
        // 设置背景颜色
        originCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 由于在FillStyleCellWriteHandler,会把OriginCellStyle和WriteCellStyle合并,会以WriteCellStyle样式为主,所有必须把WriteCellStyle设置的背景色清空
        // 具体合并规则看WriteWorkbookHolder.createCellStyle方法
        WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
        writeCellStyle.setFillForegroundColor(null);
        // 必须设置OriginCellStyle
        cellData.setOriginCellStyle(originCellStyle);
 
        WriteFont headWriteFont = new WriteFont();
        if (cell.getRowIndex() == 0) {
            headWriteFont.setFontHeightInPoints((short)14);
            headWriteFont.setFontName("Arial");
            headWriteFont.setBold(true);
        }else if (cell.getRowIndex() == 1) {
            headWriteFont.setFontHeightInPoints((short)12);
            headWriteFont.setFontName("Arial");
            headWriteFont.setBold(false);
        }
 
        cellData.getWriteCellStyle().setWriteFont(headWriteFont);
    }
 
    // 设置内容公示、内容字体样式、背景样式
    @Override
    protected void setContentCellStyle(CellWriteHandlerContext context) {
 
        // 获取和创建CellStyle
        WriteCellData<?> cellData = context.getFirstCellData();
        CellStyle originCellStyle = cellData.getOriginCellStyle();
        Cell cell = context.getCell();
 
        if (Objects.isNull(originCellStyle)) {
            originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
        }
        // 设置背景颜色
        ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 192, 0), new DefaultIndexedColorMap()));
        //System.out.println("Go to row: " +  context.getCell().getRowIndex() + ", column: " +  context.getCell().getColumnIndex());
 
        if (cell.getRowIndex() >= 2){
            ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap()));
 
            //if (cell.getColumnIndex() == 10){
            //    int actualCellRowNum =  context.getCell().getRowIndex() + 1;
            //    context.getCell().setCellFormula("I" + actualCellRowNum +"+J" + actualCellRowNum);
            //}
        }
 
        originCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        originCellStyle.setBorderLeft(BorderStyle.THIN);                    //左边框线
        originCellStyle.setBorderTop(BorderStyle.THIN);                     //顶部框线
        originCellStyle.setBorderRight(BorderStyle.THIN);                   //右边框线
        originCellStyle.setBorderBottom(BorderStyle.THIN);                  //底部框线
        // 由于在FillStyleCellWriteHandler,会把OriginCellStyle和WriteCellStyle合并,会以WriteCellStyle样式为主,所有必须把WriteCellStyle设置的背景色清空
        // 具体合并规则看WriteWorkbookHolder.createCellStyle方法
        WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
        writeCellStyle.setFillForegroundColor(null);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 必须设置OriginCellStyle
        cellData.setOriginCellStyle(originCellStyle);
 
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)12);
        headWriteFont.setFontName("宋体");
        headWriteFont.setBold(false);
        headWriteFont.setColor(IndexedColors.BLACK.getIndex());
 
        cellData.getWriteCellStyle().setWriteFont(headWriteFont);
    }
}