WriteCellStyle.java
11.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package com.taover.easyexcel.write.metadata.style;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import com.taover.easyexcel.metadata.property.FontProperty;
import com.taover.easyexcel.metadata.property.StyleProperty;
import com.taover.easyexcel.util.StringUtils;
/**
* Cell style when writing
*
* @author Jiaju Zhuang
*/
public class WriteCellStyle {
/**
* Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}.
*/
private Short dataFormat;
/**
* Set the font for this style
*/
private WriteFont writeFont;
/**
* Set the cell's using this style to be hidden
*/
private Boolean hidden;
/**
* Set the cell's using this style to be locked
*/
private Boolean locked;
/**
* Turn on or off "Quote Prefix" or "123 Prefix" for the style, which is used to tell Excel that the thing which
* looks like a number or a formula shouldn't be treated as on. Turning this on is somewhat (but not completely, see
* {@link IgnoredErrorType}) like prefixing the cell value with a ' in Excel
*/
private Boolean quotePrefix;
/**
* Set the type of horizontal alignment for the cell
*/
private HorizontalAlignment horizontalAlignment;
/**
* Set whether the text should be wrapped. Setting this flag to <code>true</code> make all content visible within a
* cell by displaying it on multiple lines
*
*/
private Boolean wrapped;
/**
* Set the type of vertical alignment for the cell
*/
private VerticalAlignment verticalAlignment;
/**
* Set the degree of rotation for the text in the cell.
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF uses values from 0 to 180 degrees. The
* implementations of this method will map between these two value-ranges accordingly, however the corresponding
* getter is returning values in the range mandated by the current type of Excel file-format that this CellStyle is
* applied to.
*/
private Short rotation;
/**
* Set the number of spaces to indent the text in the cell
*/
private Short indent;
/**
* Set the type of border to use for the left border of the cell
*/
private BorderStyle borderLeft;
/**
* Set the type of border to use for the right border of the cell
*/
private BorderStyle borderRight;
/**
* Set the type of border to use for the top border of the cell
*/
private BorderStyle borderTop;
/**
* Set the type of border to use for the bottom border of the cell
*/
private BorderStyle borderBottom;
/**
* Set the color to use for the left border
*
* @see IndexedColors
*/
private Short leftBorderColor;
/**
* Set the color to use for the right border
*
* @see IndexedColors
*
*/
private Short rightBorderColor;
/**
* Set the color to use for the top border
*
* @see IndexedColors
*
*/
private Short topBorderColor;
/**
* Set the color to use for the bottom border
*
* @see IndexedColors
*
*/
private Short bottomBorderColor;
/**
* Setting to one fills the cell with the foreground color... No idea about other values
*
* @see FillPatternType#SOLID_FOREGROUND
*/
private FillPatternType fillPatternType;
/**
* Set the background fill color.
*
* @see IndexedColors
*
*/
private Short fillBackgroundColor;
/**
* Set the foreground fill color <i>Note: Ensure Foreground color is set prior to background color.</i>
*
* @see IndexedColors
*
*/
private Short fillForegroundColor;
/**
* Controls if the Cell should be auto-sized to shrink to fit if the text is too long
*/
private Boolean shrinkToFit;
public static WriteCellStyle build(StyleProperty styleProperty, FontProperty fontProperty) {
if (styleProperty == null && fontProperty == null) {
return null;
}
WriteCellStyle writeCellStyle = new WriteCellStyle();
if (styleProperty != null) {
if (styleProperty.getDataFormat() >= 0) {
writeCellStyle.setDataFormat(styleProperty.getDataFormat());
}
writeCellStyle.setHidden(styleProperty.getHidden());
writeCellStyle.setLocked(styleProperty.getLocked());
writeCellStyle.setQuotePrefix(styleProperty.getQuotePrefix());
writeCellStyle.setHorizontalAlignment(styleProperty.getHorizontalAlignment());
writeCellStyle.setWrapped(styleProperty.getWrapped());
writeCellStyle.setVerticalAlignment(styleProperty.getVerticalAlignment());
if (styleProperty.getRotation() >= 0) {
writeCellStyle.setRotation(styleProperty.getRotation());
}
if (styleProperty.getIndent() >= 0) {
writeCellStyle.setIndent(styleProperty.getIndent());
}
writeCellStyle.setBorderLeft(styleProperty.getBorderLeft());
writeCellStyle.setBorderRight(styleProperty.getBorderRight());
writeCellStyle.setBorderTop(styleProperty.getBorderTop());
writeCellStyle.setBorderBottom(styleProperty.getBorderBottom());
if (styleProperty.getLeftBorderColor() >= 0) {
writeCellStyle.setLeftBorderColor(styleProperty.getLeftBorderColor());
}
if (styleProperty.getRightBorderColor() >= 0) {
writeCellStyle.setRightBorderColor(styleProperty.getRightBorderColor());
}
if (styleProperty.getTopBorderColor() >= 0) {
writeCellStyle.setTopBorderColor(styleProperty.getTopBorderColor());
}
if (styleProperty.getBottomBorderColor() >= 0) {
writeCellStyle.setBottomBorderColor(styleProperty.getBottomBorderColor());
}
writeCellStyle.setFillPatternType(styleProperty.getFillPatternType());
if (styleProperty.getFillBackgroundColor() >= 0) {
writeCellStyle.setFillBackgroundColor(styleProperty.getFillBackgroundColor());
}
if (styleProperty.getFillForegroundColor() >= 0) {
writeCellStyle.setFillForegroundColor(styleProperty.getFillForegroundColor());
}
writeCellStyle.setShrinkToFit(styleProperty.getShrinkToFit());
}
if (fontProperty != null) {
WriteFont writeFont = new WriteFont();
writeCellStyle.setWriteFont(writeFont);
if (!StringUtils.isEmpty(fontProperty.getFontName())) {
writeFont.setFontName(fontProperty.getFontName());
}
writeFont.setFontHeightInPoints(fontProperty.getFontHeightInPoints());
writeFont.setItalic(fontProperty.getItalic());
writeFont.setStrikeout(fontProperty.getStrikeout());
if (fontProperty.getColor() >= 0) {
writeFont.setColor(fontProperty.getColor());
}
if (fontProperty.getTypeOffset() >= 0) {
writeFont.setTypeOffset(fontProperty.getTypeOffset());
}
if (fontProperty.getUnderline() >= 0) {
writeFont.setUnderline(fontProperty.getUnderline());
}
if (fontProperty.getCharset() >= 0) {
writeFont.setCharset(fontProperty.getCharset());
}
writeFont.setBold(fontProperty.getBold());
}
return writeCellStyle;
}
public Short getDataFormat() {
return dataFormat;
}
public void setDataFormat(Short dataFormat) {
this.dataFormat = dataFormat;
}
public WriteFont getWriteFont() {
return writeFont;
}
public void setWriteFont(WriteFont writeFont) {
this.writeFont = writeFont;
}
public Boolean getHidden() {
return hidden;
}
public void setHidden(Boolean hidden) {
this.hidden = hidden;
}
public Boolean getLocked() {
return locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public Boolean getQuotePrefix() {
return quotePrefix;
}
public void setQuotePrefix(Boolean quotePrefix) {
this.quotePrefix = quotePrefix;
}
public HorizontalAlignment getHorizontalAlignment() {
return horizontalAlignment;
}
public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
this.horizontalAlignment = horizontalAlignment;
}
public Boolean getWrapped() {
return wrapped;
}
public void setWrapped(Boolean wrapped) {
this.wrapped = wrapped;
}
public VerticalAlignment getVerticalAlignment() {
return verticalAlignment;
}
public void setVerticalAlignment(VerticalAlignment verticalAlignment) {
this.verticalAlignment = verticalAlignment;
}
public Short getRotation() {
return rotation;
}
public void setRotation(Short rotation) {
this.rotation = rotation;
}
public Short getIndent() {
return indent;
}
public void setIndent(Short indent) {
this.indent = indent;
}
public BorderStyle getBorderLeft() {
return borderLeft;
}
public void setBorderLeft(BorderStyle borderLeft) {
this.borderLeft = borderLeft;
}
public BorderStyle getBorderRight() {
return borderRight;
}
public void setBorderRight(BorderStyle borderRight) {
this.borderRight = borderRight;
}
public BorderStyle getBorderTop() {
return borderTop;
}
public void setBorderTop(BorderStyle borderTop) {
this.borderTop = borderTop;
}
public BorderStyle getBorderBottom() {
return borderBottom;
}
public void setBorderBottom(BorderStyle borderBottom) {
this.borderBottom = borderBottom;
}
public Short getLeftBorderColor() {
return leftBorderColor;
}
public void setLeftBorderColor(Short leftBorderColor) {
this.leftBorderColor = leftBorderColor;
}
public Short getRightBorderColor() {
return rightBorderColor;
}
public void setRightBorderColor(Short rightBorderColor) {
this.rightBorderColor = rightBorderColor;
}
public Short getTopBorderColor() {
return topBorderColor;
}
public void setTopBorderColor(Short topBorderColor) {
this.topBorderColor = topBorderColor;
}
public Short getBottomBorderColor() {
return bottomBorderColor;
}
public void setBottomBorderColor(Short bottomBorderColor) {
this.bottomBorderColor = bottomBorderColor;
}
public FillPatternType getFillPatternType() {
return fillPatternType;
}
public void setFillPatternType(FillPatternType fillPatternType) {
this.fillPatternType = fillPatternType;
}
public Short getFillBackgroundColor() {
return fillBackgroundColor;
}
public void setFillBackgroundColor(Short fillBackgroundColor) {
this.fillBackgroundColor = fillBackgroundColor;
}
public Short getFillForegroundColor() {
return fillForegroundColor;
}
public void setFillForegroundColor(Short fillForegroundColor) {
this.fillForegroundColor = fillForegroundColor;
}
public Boolean getShrinkToFit() {
return shrinkToFit;
}
public void setShrinkToFit(Boolean shrinkToFit) {
this.shrinkToFit = shrinkToFit;
}
}