3030import java .time .LocalDate ;
3131import java .time .ZoneId ;
3232import java .util .Date ;
33+ import java .util .HashMap ;
3334import java .util .List ;
35+ import java .util .Map ;
3436import java .util .stream .Collectors ;
3537import java .util .stream .Stream ;
3638import org .apache .commons .lang3 .ArrayUtils ;
5759import com .vaadin .flow .data .binder .BeanPropertySet ;
5860import com .vaadin .flow .data .binder .PropertySet ;
5961import com .vaadin .flow .data .provider .DataProvider ;
62+ import com .vaadin .flow .function .ValueProvider ;
6063
6164/**
6265 * @author mlopez
@@ -66,6 +69,7 @@ class ExcelInputStreamFactory<T> extends BaseInputStreamFactory<T> {
6669
6770 private static final Logger LOGGER = LoggerFactory .getLogger (ExcelInputStreamFactory .class );
6871 private static final String DEFAULT_TEMPLATE = "/template.xlsx" ;
72+ private static final String COLUMN_CELLSTYLE_MAP = "colum-cellstyle-map" ;
6973
7074 public ExcelInputStreamFactory (GridExporter <T > exporter , String template ) {
7175 super (exporter , template , DEFAULT_TEMPLATE );
@@ -279,7 +283,7 @@ private void buildRow(T item, Sheet sheet, Cell startingCell) {
279283 configureAlignment (column .getTextAlign (), currentCell );
280284 }
281285 currentColumn [0 ] = currentColumn [0 ] + 1 ;
282- buildCell (value , currentCell , column );
286+ buildCell (value , currentCell , column , item );
283287 });
284288 }
285289
@@ -331,31 +335,52 @@ protected void configureAlignment(ColumnTextAlign columnTextAlign, Cell currentC
331335 }
332336 }
333337
334- private void buildCell (Object value , Cell cell , Column <T > column ) {
335- String excelFormat =
336- (String ) ComponentUtil .getData (column , GridExporter .COLUMN_EXCEL_FORMAT_DATA );
338+ @ SuppressWarnings ("unchecked" )
339+ private void buildCell (Object value , Cell cell , Column <T > column , T item ) {
340+ ValueProvider <T ,String > provider = null ;
341+ provider = (ValueProvider <T , String >) ComponentUtil .getData (column , GridExporter .COLUMN_EXCEL_FORMAT_DATA_PROVIDER );
342+ String excelFormat ;
343+ Map <String ,CellStyle > cellStyles = (Map <String , CellStyle >) ComponentUtil .getData (column , COLUMN_CELLSTYLE_MAP );
344+ if (cellStyles ==null ) {
345+ cellStyles = new HashMap <>();
346+ ComponentUtil .setData (column , COLUMN_CELLSTYLE_MAP , cellStyles );
347+ }
348+ if (provider !=null ) {
349+ excelFormat = provider .apply (item );
350+ } else {
351+ excelFormat =
352+ (String ) ComponentUtil .getData (column , GridExporter .COLUMN_EXCEL_FORMAT_DATA );
353+ }
337354 if (value == null ) {
338355 PoiHelper .setBlank (cell );
339356 } else if (value instanceof Number ) {
340357 excelFormat = (excelFormat !=null )?excelFormat :"0" ;
341- applyExcelFormat (cell , excelFormat );
342358 cell .setCellValue (((Number ) value ).doubleValue ());
359+ applyExcelFormat (cell , excelFormat , cellStyles );
343360 } else if (value instanceof Date ) {
344361 excelFormat = (excelFormat !=null )?excelFormat :"dd/MM/yyyy" ;
345- applyExcelFormat (cell , excelFormat );
362+ applyExcelFormat (cell , excelFormat , cellStyles );
346363 cell .setCellValue ((Date ) value );
347364 } else if (value instanceof LocalDate ) {
348365 excelFormat = (excelFormat !=null )?excelFormat :"dd/MM/yyyy" ;
349- applyExcelFormat (cell , excelFormat );
366+ applyExcelFormat (cell , excelFormat , cellStyles );
350367 cell .setCellValue (
351368 Date .from (((LocalDate ) value ).atStartOfDay (ZoneId .systemDefault ()).toInstant ()));
352369 } else {
353370 cell .setCellValue (value .toString ());
354371 }
355372 }
356373
357- private void applyExcelFormat (Cell cell , String excelFormat ) {
374+ private void applyExcelFormat (Cell cell , String excelFormat , Map < String , CellStyle > cellStyles ) {
358375 DataFormat format = cell .getSheet ().getWorkbook ().createDataFormat ();
376+ if (excelFormat !=null && cellStyles .get (excelFormat )==null ) {
377+ CellStyle cs = cell .getSheet ().getWorkbook ().createCellStyle ();
378+ cs .cloneStyleFrom (cell .getCellStyle ());
379+ cellStyles .put (excelFormat , cs );
380+ cell .setCellStyle (cs );
381+ } else if (excelFormat !=null ) {
382+ cell .setCellStyle (cellStyles .get (excelFormat ));
383+ }
359384 cell .getCellStyle ().setDataFormat (format .getFormat (excelFormat ));
360385 }
361386
@@ -406,7 +431,7 @@ private void fillHeaderOrFooter(
406431 (isHeader
407432 ? headerOrFooter .getLeft ()
408433 : transformToType (headerOrFooter .getLeft (), headerOrFooter .getRight ()));
409- buildCell (value , cell , headerOrFooter .getRight ());
434+ buildCell (value , cell , headerOrFooter .getRight (), null );
410435 configureAlignment (headerOrFooter .getRight ().getTextAlign (), cell );
411436 sheet .setActiveCell (
412437 new CellAddress (
0 commit comments