15
15
use Illuminate \Support \Collection ;
16
16
use Illuminate \Support \LazyCollection ;
17
17
use Maatwebsite \Excel \ExcelServiceProvider ;
18
+ use OpenSpout \Common \Entity \Style \Style ;
18
19
use Rap2hpoutre \FastExcel \FastExcel ;
19
20
use Yajra \DataTables \Contracts \DataTableButtons ;
20
21
use Yajra \DataTables \Contracts \DataTableScope ;
@@ -674,11 +675,11 @@ public function fastExcelCallback(): Closure
674
675
return function ($ row ) {
675
676
$ mapped = [];
676
677
677
- $ this ->exportColumns ()-> each ( function ( Column $ column ) use (& $ mapped , $ row ) {
678
- if ( $ column[ ' exportable ' ]) {
679
- $ mapped[ $ column [ ' title ' ]] = $ row[ $ column [ ' data ' ]];
680
- }
681
- });
678
+ $ this ->exportColumns ()
679
+ -> reject ( fn ( Column $ column) => $ column -> exportable === false )
680
+ -> each ( function ( Column $ column ) use (& $ mapped, $ row) {
681
+ $ mapped [ $ column -> title ] = $ row [ $ column -> data ];
682
+ });
682
683
683
684
return $ mapped ;
684
685
};
@@ -705,16 +706,23 @@ protected function buildFastExcelFile(): FastExcel
705
706
$ dataTable = app ()->call ([$ this , 'dataTable ' ], compact ('query ' ));
706
707
$ dataTable ->skipPaging ();
707
708
709
+ $ styles = [];
710
+ $ this ->exportColumns ()
711
+ ->reject (fn (Column $ column ) => $ column ->exportable === false || ! $ column ->exportFormat )
712
+ ->each (function (Column $ column ) use (&$ styles ) {
713
+ $ styles [$ column ->title ] = (new Style )->setFormat ($ column ->exportFormat );
714
+ });
715
+
708
716
if ($ dataTable instanceof QueryDataTable) {
709
717
$ queryGenerator = function ($ dataTable ): Generator {
710
718
foreach ($ dataTable ->getFilteredQuery ()->cursor () as $ row ) {
711
719
yield $ row ;
712
720
}
713
721
};
714
722
715
- return new FastExcel ($ queryGenerator ($ dataTable ));
723
+ return ( new FastExcel ($ queryGenerator ($ dataTable )))-> setColumnStyles ( $ styles );
716
724
}
717
725
718
- return new FastExcel ($ dataTable ->toArray ()['data ' ]);
726
+ return ( new FastExcel ($ dataTable ->toArray ()['data ' ]))-> setColumnStyles ( $ styles );
719
727
}
720
728
}
0 commit comments