jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.bstek.bdf2.export.excel.style;
 
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
 
import com.bstek.bdf2.export.model.ReportTitle;
import com.bstek.bdf2.export.model.ReportTitleStyle;
 
public class TitleStyleBuilder extends AbstractStyleBuilder {
 
    public CellStyle builder(ReportTitle reportTitle, Workbook wb) {
        if (reportTitle.getStyle() == null) {
            return null;
        }
        ReportTitleStyle style = reportTitle.getStyle();
        int[] bgColor = style.getBgColor();
        int[] fontColor = style.getFontColor();
        int fontSize = style.getFontSize();
        if (wb instanceof HSSFWorkbook) {
            return createHSSFCellStyle(wb, bgColor, fontColor, fontSize);
        } else if (wb instanceof SXSSFWorkbook) {
            return createXSSFCellStyle(wb, bgColor, fontColor, fontSize);
        }
        return null;
    }
 
    private HSSFCellStyle createHSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
        HSSFWorkbook workbook = (HSSFWorkbook) wb;
        HSSFPalette palette = workbook.getCustomPalette();
        
        palette.setColorAtIndex((short) 9, (byte) fontColor[0], (byte) fontColor[1], (byte) fontColor[2]);
        palette.setColorAtIndex((short) 10, (byte) bgColor[0], (byte) bgColor[1], (byte) bgColor[2]);
 
        HSSFFont titleFont = workbook.createFont();
        titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
        titleFont.setFontName("宋体");
        titleFont.setColor((short) 9);
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titleFont.setFontHeightInPoints((short) fontSize);
 
        HSSFCellStyle titleStyle = (HSSFCellStyle) createBorderCellStyle(workbook, true);
        titleStyle.setFont(titleFont);
        titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        titleStyle.setFillForegroundColor((short) 10);
        titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
        titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
 
        return titleStyle;
    }
 
    private XSSFCellStyle createXSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
        SXSSFWorkbook workbook = (SXSSFWorkbook) wb;
        XSSFFont titleFont = (XSSFFont) workbook.createFont();
        titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
        titleFont.setFontName("宋体");
 
        XSSFColor color9 = new XSSFColor(new java.awt.Color(fontColor[0], fontColor[1], fontColor[2]));
        XSSFColor color10 = new XSSFColor(new java.awt.Color(bgColor[0], bgColor[1], bgColor[2]));
        
        if (!(fontColor[0] == 0 && fontColor[1] == 0 && fontColor[2] == 0)) {
            titleFont.setColor(color9);
        }
        
        //titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titleFont.setFontHeightInPoints((short) fontSize);
 
        XSSFCellStyle titleStyle = (XSSFCellStyle) createBorderCellStyle(workbook, true);
        titleStyle.setFont(titleFont);
        titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        titleStyle.setFillForegroundColor(color10);
        titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
        titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        
        titleStyle.setBorderBottom((short) 0);
        titleStyle.setBorderLeft((short) 0);
        titleStyle.setBorderTop((short) 0);
 
        return titleStyle;
    }
 
}