-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellCreator.java
416 lines (345 loc) · 16 KB
/
CellCreator.java
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package fr.javatronic.poiwrapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class CellCreator {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CellCreator.class);
public final static short BOLDWEIGHT_BOLD = 0x2bc;
private static int threshold = Integer.MAX_VALUE;
public static void setThreshold(int threshold) {
CellCreator.threshold = threshold;
}
// common CellStyle methods
private static void setCellStyle(@Nonnull Cell cell,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
if (style != null || border != null || dataformat != null) {
cell.setCellStyle(createCellStyle(cell.getRow(), style, border, dataformat));
}
}
public static CellStyle createCellStyle(@Nonnull Row row,
@Nullable CellStyleDescriptor cellStyleDescriptor,
@Nullable Border border, @Nullable DataFormat dataformat) {
Workbook wb = row.getSheet().getWorkbook();
CellStyle style = wb.createCellStyle();
setFont(wb, style, cellStyleDescriptor);
setBGColor(style, cellStyleDescriptor);
setAlignements(style, cellStyleDescriptor);
setBorders(style, border);
if (dataformat != null) {
style.setDataFormat(dataformat.getFormat());
}
if (cellStyleDescriptor != null) {
style.setWrapText(cellStyleDescriptor.isWrapText());
}
return style;
}
private static void setAlignements(@Nonnull CellStyle style, @Nullable CellStyleDescriptor descriptor) {
if (descriptor == null) {
return;
}
style.setVerticalAlignment(descriptor.getAlignV());
if (descriptor.getAlignH() != null) {
style.setAlignment(descriptor.getAlignH());
}
}
private static void setFont(@Nonnull Workbook wb, @Nonnull CellStyle style, @Nullable CellStyleDescriptor descriptor) {
if (descriptor == null) {
return;
}
Font font = wb.createFont();
style.setFont(font);
font.setColor(descriptor.getFgColor().getIndex());
if (descriptor.getFontSize() != null) {
font.setFontHeightInPoints(descriptor.getFontSize());
}
font.setFontName(descriptor.getFontName());
font.setItalic(descriptor.getFontStyles().contains(CellStyleDescriptor.FontStyle.ITALIC));
if (descriptor.getFontStyles().contains(CellStyleDescriptor.FontStyle.BOLD)) {
font.setBoldweight(CellCreator.BOLDWEIGHT_BOLD);
}
}
public static void setBorders(@Nonnull CellStyle cellStyle, @Nullable Border border) {
if (border == null) {
return;
}
cellStyle.setBorderLeft(border.getLeft());
cellStyle.setBorderRight(border.getRight());
cellStyle.setBorderBottom(border.getDown());
cellStyle.setBorderTop(border.getUp());
}
// region support
private static void setBorders(@Nonnull MergedRegion region, @Nullable Border border) {
if (border == null || (MergedRegion.isDebugEnabled() && region.getCounter() >= threshold)) {
return;
}
setLeftBorder(region, border);
setRightBorder(region, border);
setBottomBorder(region, border);
setTopBorder(region, border);
}
private static void setLeftBorder(@Nonnull MergedRegion region, @Nonnull Border border) {
int rowStart = region.getRow().getRowNum() + 1; // +1 to not set style on root cell
int rowEnd = region.getRowEnd();
int column = region.getColStart();
for (int i = rowStart; i <= rowEnd; i++) {
Cell cell = getOrCreateCell(region.getRow().getSheet(), i, column);
if (!isRootCell(region, cell)) {
setCellStyle(cell, null, border, null);
}
}
}
private static void setTopBorder(@Nonnull MergedRegion region, @Nonnull Border border) {
int colStart = region.getColStart();
int colEnd = region.getColEnd();
int rowIndex = region.getRow().getRowNum();
for (int i = colStart; i <= colEnd; i++) {
Cell cell = getOrCreateCell(region.getRow().getSheet(), rowIndex, i);
if (!isRootCell(region, cell)) {
setCellStyle(cell, null, border, null);
}
}
}
private static void setRightBorder(@Nonnull MergedRegion region, @Nonnull Border border) {
int rowStart = region.getRow().getRowNum();
int rowEnd = region.getRowEnd();
int column = region.getColEnd();
for (int i = rowStart; i <= rowEnd; i++) {
Cell cell = getOrCreateCell(region.getRow().getSheet(), i, column);
if (!isRootCell(region, cell)) {
setCellStyle(cell, null, border, null);
}
}
}
private static void setBottomBorder(@Nonnull MergedRegion region, @Nonnull Border border) {
int colStart = region.getColStart();
int colEnd = region.getColEnd();
int rowIndex = region.getRowEnd();
for (int i = colStart; i <= colEnd; i++) {
Cell cell = getOrCreateCell(region.getRow().getSheet(), rowIndex, i);
if (!isRootCell(region, cell)) {
setCellStyle(cell, null, border, null);
}
}
}
/**
* Tests whether the specified cell is the root cell of the specified MergeRegion
*/
private static boolean isRootCell(@Nonnull MergedRegion region, @Nonnull Cell cell) {
return cell.getRow().getRowNum() == region.getRow().getRowNum()
&& cell.getColumnIndex() == region.getColStart();
}
private static Cell getOrCreateCell(Sheet sheet, int rowIndex, int column) {
return getOrCreateCell(getOrCreateRow(sheet, rowIndex), column);
}
private static Row getOrCreateRow(Sheet sheet, int rowIndex) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
return sheet.createRow(rowIndex);
}
return row;
}
private static Cell getOrCreateCell(Row row, int column) {
Cell cell = row.getCell(column);
if (cell == null) {
return row.createCell(column);
}
return cell;
}
private static void setBGColor(@Nonnull CellStyle style, @Nullable CellStyleDescriptor descriptor) {
if (descriptor != null && descriptor.getBgColor() != null) {
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(descriptor.getBgColor().getIndex());
}
}
/**
* Warning: merged region must be created <strong>after</strong> borders have been set with method
* {@link #setBorders(MergedRegion, Border)}.
*/
private static void createMergedRegion(MergedRegion region) {
if (MergedRegion.isDebugEnabled() && region.getCounter() >= threshold) {
return;
}
LOGGER.debug("creating region {} for threshold {}", region.getCounter(), threshold);
CellRangeAddress range = new CellRangeAddress(region.getRow().getRowNum(), region.getRowEnd(), region.getColStart(), region.getColEnd());
region.getRow().getSheet().addMergedRegion(range);
}
// RichTextString
public static Cell createCell(Row row, int columnIndex, RichTextString value,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
Cell cell = row.createCell(columnIndex);
cell.setCellValue(value);
setCellStyle(cell, style, border, dataformat);
return cell;
}
public static Cell createCell(Row row, int columnIndex, RichTextString value, CellStyleDescriptor style, Border border) {
return createCell(row, columnIndex, value, style, border, null);
}
public static Cell createCell(Row row, int columnIndex, RichTextString value, CellStyleDescriptor style, DataFormat dataformat) {
return createCell(row, columnIndex, value, style, null, dataformat);
}
public static Cell createCell(Row row, int columnIndex, RichTextString value, CellStyleDescriptor style) {
return createCell(row, columnIndex, value, style, null, null);
}
public static Cell createCell(Row row, int columnIndex, RichTextString value) {
return createCell(row, columnIndex, value, null, null, null);
}
/**
* Creates a cell with a {@link RichTextString} value which spans over several merged cells (eg. a merged region).
*/
public static Cell createCell(@Nonnull MergedRegion region, RichTextString value,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
Cell cell = createCell(region.getRow(), region.getColStart(), value, style, border, dataformat);
setBorders(region, border);
createMergedRegion(region);
return cell;
}
public static Cell createCell(@Nonnull MergedRegion region, RichTextString value, @Nullable CellStyleDescriptor style, @Nullable Border border) {
return createCell(region, value, style, border, null);
}
public static Cell createCell(@Nonnull MergedRegion region, RichTextString value, @Nullable CellStyleDescriptor style, @Nullable DataFormat dataformat) {
return createCell(region, value, style, null, dataformat);
}
public static Cell createCell(@Nonnull MergedRegion region, RichTextString value, @Nullable CellStyleDescriptor style) {
return createCell(region, value, style, null, null);
}
public static Cell createCell(@Nonnull MergedRegion region, RichTextString value) {
return createCell(region, value, null, null, null);
}
// String value
public static Cell createCell(Row row, int columnIndex, String value, CellStyleDescriptor style, Border border, DataFormat dataformat) {
Cell cell = row.createCell(columnIndex);
cell.setCellValue(value);
setCellStyle(cell, style, border, dataformat);
return cell;
}
public static Cell createCell(Row row, int columnIndex, String value, CellStyleDescriptor style, Border border) {
return createCell(row, columnIndex, value, style, border, null);
}
public static Cell createCell(Row row, int columnIndex, String value, CellStyleDescriptor style, DataFormat dataformat) {
return createCell(row, columnIndex, value, style, null, dataformat);
}
public static Cell createCell(Row row, int columnIndex, String value, CellStyleDescriptor style) {
return createCell(row, columnIndex, value, style, null, null);
}
public static Cell createCell(Row row, int columnIndex, String value) {
return createCell(row, columnIndex, value, null, null, null);
}
/**
* Creates a cell with a String value which spans over several merged cells (eg. a merged region).
*/
public static Cell createCell(@Nonnull MergedRegion region, String value,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
Cell cell = createCell(region.getRow(), region.getColStart(), value, style, border, dataformat);
setBorders(region, border);
createMergedRegion(region);
return cell;
}
public static Cell createCell(@Nonnull MergedRegion region, String value, @Nullable CellStyleDescriptor style, @Nullable Border border) {
return createCell(region, value, style, border, null);
}
public static Cell createCell(@Nonnull MergedRegion region, String value, @Nullable CellStyleDescriptor style, @Nullable DataFormat dataformat) {
return createCell(region, value, style, null, dataformat);
}
public static Cell createCell(@Nonnull MergedRegion region, String value, @Nullable CellStyleDescriptor style) {
return createCell(region, value, style, null, null);
}
public static Cell createCell(@Nonnull MergedRegion region, String value) {
return createCell(region, value, null, null, null);
}
// float value
public static Cell createCell(Row row, int columnIndex, float value, CellStyleDescriptor style, Border border, DataFormat dataformat) {
Cell cell = row.createCell(columnIndex);
cell.setCellValue(value);
setCellStyle(cell, style, border, dataformat);
return cell;
}
public static Cell createCell(Row row, int columnIndex, float value, CellStyleDescriptor style, Border border) {
return createCell(row, columnIndex, value, style, border, null);
}
public static Cell createCell(Row row, int columnIndex, float value, CellStyleDescriptor style, DataFormat dataformat) {
return createCell(row, columnIndex, value, style, null, dataformat);
}
public static Cell createCell(Row row, int columnIndex, float value, CellStyleDescriptor style) {
return createCell(row, columnIndex, value, style, null, null);
}
public static Cell createCell(Row row, int columnIndex, float value) {
return createCell(row, columnIndex, value, null, null, null);
}
/**
* Creates a cell with a float value which spans over several merged cells (eg. a merged region).
*/
public static Cell createCell(@Nonnull MergedRegion region, float value,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
Cell cell = createCell(region.getRow(), region.getColStart(), value, style, border, dataformat);
setBorders(region, border);
createMergedRegion(region);
return cell;
}
public static Cell createCell(@Nonnull MergedRegion region, float value, @Nullable CellStyleDescriptor style, @Nullable Border border) {
return createCell(region, value, style, border, null);
}
public static Cell createCell(@Nonnull MergedRegion region, float value, @Nullable CellStyleDescriptor style, @Nullable DataFormat dataformat) {
return createCell(region, value, style, null, dataformat);
}
public static Cell createCell(@Nonnull MergedRegion region, float value, @Nullable CellStyleDescriptor style) {
return createCell(region, value, style, null, null);
}
public static Cell createCell(@Nonnull MergedRegion region, float value) {
return createCell(region, value, null, null, null);
}
// int value
public static Cell createCell(Row row, int columnIndex, int value, CellStyleDescriptor style, Border border, DataFormat dataformat) {
Cell cell = row.createCell(columnIndex);
cell.setCellValue(value);
setCellStyle(cell, style, border, dataformat);
return cell;
}
public static Cell createCell(Row row, int columnIndex, int value, CellStyleDescriptor style, Border border) {
return createCell(row, columnIndex, value, style, border, null);
}
public static Cell createCell(Row row, int columnIndex, int value, CellStyleDescriptor style, DataFormat dataformat) {
return createCell(row, columnIndex, value, style, null, dataformat);
}
public static Cell createCell(Row row, int columnIndex, int value, CellStyleDescriptor style) {
return createCell(row, columnIndex, value, style, null, null);
}
public static Cell createCell(Row row, int columnIndex, int value) {
return createCell(row, columnIndex, value, null, null, null);
}
/**
* Creates a cell with a int value which spans over several merged cells (eg. a merged region).
*/
public static Cell createCell(@Nonnull MergedRegion region, int value,
@Nullable CellStyleDescriptor style, @Nullable Border border, @Nullable DataFormat dataformat) {
Cell cell = createCell(region.getRow(), region.getColStart(), value, style, border, dataformat);
setBorders(region, border);
createMergedRegion(region);
return cell;
}
public static Cell createCell(@Nonnull MergedRegion region, int value, @Nullable CellStyleDescriptor style, @Nullable Border border) {
return createCell(region, value, style, border, null);
}
public static Cell createCell(@Nonnull MergedRegion region, int value, @Nullable CellStyleDescriptor style, @Nullable DataFormat dataformat) {
return createCell(region, value, style, null, dataformat);
}
public static Cell createCell(@Nonnull MergedRegion region, int value, @Nullable CellStyleDescriptor style) {
return createCell(region, value, style, null, null);
}
public static Cell createCell(@Nonnull MergedRegion region, int value) {
return createCell(region, value, null, null, null);
}
/**
* Un pourcentage dans excel doit être exprimé en fraction (ie. 100% = 1, 5% = 0,05)
*/
public static float formatRatio(float remise) {
if (remise == 0f) {
return remise;
}
if (remise >= 1) {
return remise / 100f;
}
return remise;
}
}