@@ -70,7 +70,8 @@ class ExcelInputStreamFactory<T> extends BaseInputStreamFactory<T> {
70
70
private static final Logger LOGGER = LoggerFactory .getLogger (ExcelInputStreamFactory .class );
71
71
private static final String DEFAULT_TEMPLATE = "/template.xlsx" ;
72
72
private static final String COLUMN_CELLSTYLE_MAP = "colum-cellstyle-map" ;
73
-
73
+ private static enum ExcelCellType {HEADER ,CELL ,FOOTER };
74
+
74
75
public ExcelInputStreamFactory (GridExporter <T > exporter , String template ) {
75
76
super (exporter , template , DEFAULT_TEMPLATE );
76
77
}
@@ -281,7 +282,7 @@ private void buildRow(T item, Sheet sheet, Cell startingCell) {
281
282
currentCell = startingCell .getRow ().createCell (currentColumn [0 ]);
282
283
currentCell .setCellStyle (startingCell .getCellStyle ());
283
284
284
- configureAlignment (column . getTextAlign () , currentCell );
285
+ configureAlignment (column , currentCell , ExcelCellType . CELL );
285
286
}
286
287
currentColumn [0 ] = currentColumn [0 ] + 1 ;
287
288
buildCell (value , currentCell , column , item );
@@ -320,20 +321,51 @@ private Object transformToType(Object value, Column<T> column) {
320
321
return result ;
321
322
}
322
323
323
- protected void configureAlignment (ColumnTextAlign columnTextAlign , Cell currentCell ) {
324
+ private void configureAlignment (Column <T > column , Cell currentCell , ExcelCellType type ) {
325
+ ColumnTextAlign columnTextAlign = column .getTextAlign ();
324
326
switch (columnTextAlign ) {
325
327
case START :
326
- currentCell .getCellStyle ().setAlignment (HorizontalAlignment .LEFT );
328
+ if (!currentCell .getCellStyle ().getAlignment ().equals (HorizontalAlignment .LEFT )) {
329
+ currentCell .setCellStyle (
330
+ createOrRetrieveCellStyle (HorizontalAlignment .LEFT , currentCell , column , type ));
331
+ }
327
332
break ;
328
333
case CENTER :
329
- currentCell .getCellStyle ().setAlignment (HorizontalAlignment .CENTER );
334
+ if (!currentCell .getCellStyle ().getAlignment ().equals (HorizontalAlignment .CENTER )) {
335
+ currentCell .setCellStyle (
336
+ createOrRetrieveCellStyle (HorizontalAlignment .CENTER , currentCell , column , type ));
337
+ }
330
338
break ;
331
339
case END :
332
- currentCell .getCellStyle ().setAlignment (HorizontalAlignment .RIGHT );
340
+ if (!currentCell .getCellStyle ().getAlignment ().equals (HorizontalAlignment .RIGHT )) {
341
+ currentCell .setCellStyle (
342
+ createOrRetrieveCellStyle (HorizontalAlignment .RIGHT , currentCell , column , type ));
343
+ }
333
344
break ;
334
345
default :
335
- currentCell .getCellStyle ().setAlignment (HorizontalAlignment .LEFT );
346
+ currentCell .setCellStyle (currentCell .getCellStyle ());
347
+ }
348
+ }
349
+
350
+ @ SuppressWarnings ("unchecked" )
351
+ private CellStyle createOrRetrieveCellStyle (HorizontalAlignment alignment , Cell currentCell ,
352
+ Column <T > column , ExcelCellType type ) {
353
+ Map <String , CellStyle > cellStyles =
354
+ (Map <String , CellStyle >) ComponentUtil .getData (column , COLUMN_CELLSTYLE_MAP );
355
+ if (cellStyles == null ) {
356
+ cellStyles = new HashMap <>();
357
+ ComponentUtil .setData (column , COLUMN_CELLSTYLE_MAP , cellStyles );
358
+ }
359
+ CellStyle cellStyle ;
360
+ if (cellStyles .get (type .name ()) == null ) {
361
+ cellStyle = currentCell .getSheet ().getWorkbook ().createCellStyle ();
362
+ cellStyle .cloneStyleFrom (currentCell .getCellStyle ());
363
+ cellStyle .setAlignment (alignment );
364
+ cellStyles .put (type .name (), cellStyle );
365
+ } else {
366
+ cellStyle = cellStyles .get (type .name ());
336
367
}
368
+ return cellStyle ;
337
369
}
338
370
339
371
@ SuppressWarnings ("unchecked" )
@@ -437,7 +469,7 @@ private void fillHeaderOrFooter(
437
469
? headerOrFooter .getLeft ()
438
470
: transformToType (headerOrFooter .getLeft (), headerOrFooter .getRight ()));
439
471
buildCell (value , cell , headerOrFooter .getRight (), null );
440
- configureAlignment (headerOrFooter .getRight (). getTextAlign () , cell );
472
+ configureAlignment (headerOrFooter .getRight (), cell , isHeader ? ExcelCellType . HEADER : ExcelCellType . FOOTER );
441
473
sheet .setActiveCell (
442
474
new CellAddress (
443
475
sheet .getActiveCell ().getRow (), sheet .getActiveCell ().getColumn () + 1 ));
0 commit comments