yanghb
2024-12-17 1287337fd0b0c156ec79712f9a600ebeffefe3a6
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
package com.zzg.common.utils;
 
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
 
/**
 *
 */
public class ExcelStyleUtils {
 
    /**
     * 设置字体
     *
     * @param font
     * @param fontName
     * @param fontHeightInPoints
     */
    public static void setFont(Font font, String fontName, short fontHeightInPoints) {
        font.setFontName(fontName);
        font.setFontHeightInPoints(fontHeightInPoints);
    }
 
    public static void setFontStyle(CellStyle style, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FillPatternType fillPatternType) {
        // 设置水平居中
        style.setAlignment(horizontalAlignment);
        // 设置垂直居中
        style.setVerticalAlignment(verticalAlignment);
        style.setFillPattern(fillPatternType);
    }
 
 
    public static void setBorder(CellStyle style, BorderStyle border) {
        setBorder(style, border, border, border, border);
    }
 
    /**
     * 设置边框样式
     *
     * @param style
     * @param borderTop
     * @param borderBottom
     * @param borderLeft
     * @param borderRight
     */
    public static void setBorder(CellStyle style, BorderStyle borderTop, BorderStyle borderBottom, BorderStyle borderLeft, BorderStyle borderRight) {
        style.setBorderTop(borderTop);
        style.setBorderBottom(borderBottom);
        style.setBorderLeft(borderLeft);
        style.setBorderRight(borderRight);
    }
 
    public static void setBorderColor(CellStyle style, short color) {
        setBorderColor(style, color, color, color, color);
    }
 
    /**
     * 设置边框颜色
     *
     * @param style
     * @param borderTop
     * @param borderBottom
     * @param borderLeft
     * @param borderRight
     */
    public static void setBorderColor(CellStyle style, short borderTop, short borderBottom, short borderLeft, short borderRight) {
        style.setTopBorderColor(borderTop);
        style.setBottomBorderColor(borderBottom);
        style.setLeftBorderColor(borderLeft);
        style.setRightBorderColor(borderRight);
    }
 
    /**
     * 设置样式
     */
    public static void setStyle(Sheet sheet, CellRangeAddress range, CellStyle style) {
        for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
            Row r = sheet.getRow(row);
            if (r == null) {
                r = sheet.createRow(row);
            }
            for (int col = range.getFirstColumn(); col <= range.getLastColumn(); col++) {
                Cell cell = r.getCell(col);
                if (cell == null) {
                    cell = r.createCell(col);
                }
                cell.setCellStyle(style);
            }
        }
    }
 
}