@@ -70,7 +70,8 @@ class ExcelInputStreamFactory<T> extends BaseInputStreamFactory<T> {
7070 private static final Logger LOGGER = LoggerFactory .getLogger (ExcelInputStreamFactory .class );
7171 private static final String DEFAULT_TEMPLATE = "/template.xlsx" ;
7272 private static final String COLUMN_CELLSTYLE_MAP = "colum-cellstyle-map" ;
73-
73+ private static enum ExcelCellType {HEADER ,CELL ,FOOTER };
74+
7475 public ExcelInputStreamFactory (GridExporter <T > exporter , String template ) {
7576 super (exporter , template , DEFAULT_TEMPLATE );
7677 }
@@ -281,7 +282,7 @@ private void buildRow(T item, Sheet sheet, Cell startingCell) {
281282 currentCell = startingCell .getRow ().createCell (currentColumn [0 ]);
282283 currentCell .setCellStyle (startingCell .getCellStyle ());
283284
284- configureAlignment (column . getTextAlign () , currentCell );
285+ configureAlignment (column , currentCell , ExcelCellType . CELL );
285286 }
286287 currentColumn [0 ] = currentColumn [0 ] + 1 ;
287288 buildCell (value , currentCell , column , item );
@@ -320,20 +321,51 @@ private Object transformToType(Object value, Column<T> column) {
320321 return result ;
321322 }
322323
323- protected void configureAlignment (ColumnTextAlign columnTextAlign , Cell currentCell ) {
324+ private void configureAlignment (Column <T > column , Cell currentCell , ExcelCellType type ) {
325+ ColumnTextAlign columnTextAlign = column .getTextAlign ();
324326 switch (columnTextAlign ) {
325327 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+ }
327332 break ;
328333 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+ }
330338 break ;
331339 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+ }
333344 break ;
334345 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 ());
336367 }
368+ return cellStyle ;
337369 }
338370
339371 @ SuppressWarnings ("unchecked" )
@@ -437,7 +469,7 @@ private void fillHeaderOrFooter(
437469 ? headerOrFooter .getLeft ()
438470 : transformToType (headerOrFooter .getLeft (), headerOrFooter .getRight ()));
439471 buildCell (value , cell , headerOrFooter .getRight (), null );
440- configureAlignment (headerOrFooter .getRight (). getTextAlign () , cell );
472+ configureAlignment (headerOrFooter .getRight (), cell , isHeader ? ExcelCellType . HEADER : ExcelCellType . FOOTER );
441473 sheet .setActiveCell (
442474 new CellAddress (
443475 sheet .getActiveCell ().getRow (), sheet .getActiveCell ().getColumn () + 1 ));
0 commit comments