liujie
2026-01-14 f976a39b294f8c7dd60bcd2c661983c0dcc97fc6
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
package com.estate.expert.admin.stat.report.render;
 
import com.deepoove.poi.util.TableTools;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
 
import java.math.BigInteger;
 
/**
 * 渲染帮助类
 */
@Log4j2
public class RenderHelper {
 
    /**
     * 渲染单元格
     * @param row 行
     * @param text 文本
     */
    public static void renderCell(XWPFTableRow row, String text) {
        var cell = row.createCell();
        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
        renderContentCell(cell, text, false, null);
    }
 
    /**
     * 渲染单元格
     * @param row 行
     * @param text 文本
     * @param textColor 文本颜色
     */
    public static void renderCell(XWPFTableRow row, String text, String textColor) {
        var cell = row.createCell();
        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
        renderContentCell(cell, text, false, textColor);
    }
 
    /**
     * 渲染内容单元格
     * @param cell 单元格
     * @param text 文本
     * @param isTitleCell 是否是标题单元格
     */
    public static void renderContentCell(XWPFTableCell cell, String text, boolean isTitleCell) {
        renderContentCell(cell, text, isTitleCell, null);
    }
 
    /**
     * 渲染内容单元格
     * @param cell 单元格
     * @param text 文本
     * @param isTitleCell 是否是标题单元格
     * @param textColor 文本颜色
     */
    public static void renderContentCell(XWPFTableCell cell, String text, boolean isTitleCell, String textColor){
        var borders = cell.getCTTc().addNewTcPr().addNewTcBorders();
        borders.addNewTop().setVal(STBorder.SINGLE);
        borders.addNewBottom().setVal(STBorder.SINGLE);
        borders.addNewLeft().setVal(STBorder.SINGLE);
        borders.addNewRight().setVal(STBorder.SINGLE);
 
        // 设置边框的颜色和宽度
        borders.getTop().setColor("000000"); // 设置为黑色
        borders.getTop().setSz(BigInteger.valueOf(4)); // 设置宽度为4
        borders.getBottom().setColor("000000");
        borders.getBottom().setSz(BigInteger.valueOf(4));
        borders.getLeft().setColor("000000");
        borders.getLeft().setSz(BigInteger.valueOf(4));
        borders.getRight().setColor("000000");
        borders.getRight().setSz(BigInteger.valueOf(4));
 
        // 设置字体样式
        var run = cell.getParagraphs().get(0).createRun();
        run.setFontFamily("宋体"); // 设置字体为宋体
        run.setFontSize(12); // 设置字体大小为小四(12磅)
        run.setText(text);
 
        if(textColor != null) {
            run.setColor(textColor);
        } else {
            run.setColor("000000");
        }
 
        // 设置单元格的水平和垂直对齐方式
        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
 
        // 设置单元格的背景颜色
        if(isTitleCell)
            cell.setColor("D9D9D9");
    }
 
    /**
     * 合并垂直单元格
     * @param table 表格
     * @param startRow 开始行
     * @param col 列
     */
    public static void mergeCellsVertically(XWPFTable table, int startRow, int col) {
        int start = 0;
        int size = table.getRows().size();
        String content = table.getRow(startRow).getCell(col).getText();
        for(int i = 1; i < size; i++){
            var cell = table.getRow(i).getCell(col);
            String cellText = cell.getText();
            if(cellText.equals(content)){
                continue;
            }
            if(i - start > 1) {
//                log.info("Merge cells from {} to {}", start, i - 1);
                TableTools.mergeCellsVertically(table, col, start, i - 1);
            }
            start = i;
            content = cellText;
        }
 
        // Merge the last group of cells
        if(size - start > 1){
//            log.info("Merge cells from {} to {}", start, size - 1);
            TableTools.mergeCellsVertically(table, col, start, size - 1);
        }
    }
}