StyleUtil.java
6.93 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.taover.easyexcel.util;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import com.taover.easyexcel.write.metadata.style.WriteCellStyle;
import com.taover.easyexcel.write.metadata.style.WriteFont;
/**
* @author jipengfei
*/
public class StyleUtil {
private StyleUtil() {}
/**
* @param workbook
* @return
*/
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setWrapText(true);
newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
newCellStyle.setAlignment(HorizontalAlignment.CENTER);
newCellStyle.setLocked(true);
newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
newCellStyle.setBorderTop(BorderStyle.THIN);
newCellStyle.setBorderBottom(BorderStyle.THIN);
newCellStyle.setBorderLeft(BorderStyle.THIN);
newCellStyle.setBorderRight(BorderStyle.THIN);
return newCellStyle;
}
/**
* Build head cell style
*
* @param workbook
* @param writeCellStyle
* @return
*/
public static CellStyle buildHeadCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
CellStyle cellStyle = buildDefaultCellStyle(workbook);
if (writeCellStyle == null) {
return cellStyle;
}
buildCellStyle(workbook, cellStyle, writeCellStyle, true);
return cellStyle;
}
/**
* Build content cell style
*
* @param workbook
* @param writeCellStyle
* @return
*/
public static CellStyle buildContentCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
CellStyle cellStyle = workbook.createCellStyle();
if (writeCellStyle == null) {
return cellStyle;
}
buildCellStyle(workbook, cellStyle, writeCellStyle, false);
return cellStyle;
}
private static void buildCellStyle(Workbook workbook, CellStyle cellStyle, WriteCellStyle writeCellStyle,
boolean isHead) {
buildFont(workbook, cellStyle, writeCellStyle.getWriteFont(), isHead);
if (writeCellStyle.getDataFormat() != null) {
cellStyle.setDataFormat(writeCellStyle.getDataFormat());
}
if (writeCellStyle.getHidden() != null) {
cellStyle.setHidden(writeCellStyle.getHidden());
}
if (writeCellStyle.getLocked() != null) {
cellStyle.setLocked(writeCellStyle.getLocked());
}
if (writeCellStyle.getQuotePrefix() != null) {
cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
}
if (writeCellStyle.getHorizontalAlignment() != null) {
cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
}
if (writeCellStyle.getWrapped() != null) {
cellStyle.setWrapText(writeCellStyle.getWrapped());
}
if (writeCellStyle.getVerticalAlignment() != null) {
cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
}
if (writeCellStyle.getRotation() != null) {
cellStyle.setRotation(writeCellStyle.getRotation());
}
if (writeCellStyle.getIndent() != null) {
cellStyle.setIndention(writeCellStyle.getIndent());
}
if (writeCellStyle.getBorderLeft() != null) {
cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
}
if (writeCellStyle.getBorderRight() != null) {
cellStyle.setBorderRight(writeCellStyle.getBorderRight());
}
if (writeCellStyle.getBorderTop() != null) {
cellStyle.setBorderTop(writeCellStyle.getBorderTop());
}
if (writeCellStyle.getBorderBottom() != null) {
cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
}
if (writeCellStyle.getLeftBorderColor() != null) {
cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
}
if (writeCellStyle.getRightBorderColor() != null) {
cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
}
if (writeCellStyle.getTopBorderColor() != null) {
cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
}
if (writeCellStyle.getBottomBorderColor() != null) {
cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
}
if (writeCellStyle.getFillPatternType() != null) {
cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
}
if (writeCellStyle.getFillBackgroundColor() != null) {
cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
}
if (writeCellStyle.getFillForegroundColor() != null) {
cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
}
if (writeCellStyle.getShrinkToFit() != null) {
cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
}
}
private static void buildFont(Workbook workbook, CellStyle cellStyle, WriteFont writeFont, boolean isHead) {
Font font = null;
if (isHead) {
font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
cellStyle.setFont(font);
}
if (writeFont == null) {
return;
}
if (!isHead) {
font = workbook.createFont();
cellStyle.setFont(font);
}
if (writeFont.getFontName() != null) {
font.setFontName(writeFont.getFontName());
}
if (writeFont.getFontHeightInPoints() != null) {
font.setFontHeightInPoints(writeFont.getFontHeightInPoints());
}
if (writeFont.getItalic() != null) {
font.setItalic(writeFont.getItalic());
}
if (writeFont.getStrikeout() != null) {
font.setStrikeout(writeFont.getStrikeout());
}
if (writeFont.getColor() != null) {
font.setColor(writeFont.getColor());
}
if (writeFont.getTypeOffset() != null) {
font.setTypeOffset(writeFont.getTypeOffset());
}
if (writeFont.getUnderline() != null) {
font.setUnderline(writeFont.getUnderline());
}
if (writeFont.getCharset() != null) {
font.setCharSet(writeFont.getCharset());
}
if (writeFont.getBold() != null) {
font.setBold(writeFont.getBold());
}
}
}