1515use Illuminate \Support \Collection ;
1616use Illuminate \Support \LazyCollection ;
1717use Maatwebsite \Excel \ExcelServiceProvider ;
18+ use OpenSpout \Common \Entity \Style \Style ;
1819use Rap2hpoutre \FastExcel \FastExcel ;
1920use Yajra \DataTables \Contracts \DataTableButtons ;
2021use Yajra \DataTables \Contracts \DataTableScope ;
@@ -674,11 +675,11 @@ public function fastExcelCallback(): Closure
674675 return function ($ row ) {
675676 $ mapped = [];
676677
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+ });
682683
683684 return $ mapped ;
684685 };
@@ -705,16 +706,25 @@ protected function buildFastExcelFile(): FastExcel
705706 $ dataTable = app ()->call ([$ this , 'dataTable ' ], compact ('query ' ));
706707 $ dataTable ->skipPaging ();
707708
709+ $ styles = [];
710+ $ this ->exportColumns ()
711+ ->reject (fn (Column $ column ) => $ column ->exportable === false )
712+ ->each (function (Column $ column ) use (&$ styles ) {
713+ if ($ column ->exportFormat ) {
714+ $ styles [$ column ->title ] = (new Style )->setFormat ($ column ->exportFormat );
715+ }
716+ });
717+ 708718 if ($ dataTable instanceof QueryDataTable) {
709719 $ queryGenerator = function ($ dataTable ): Generator {
710720 foreach ($ dataTable ->getFilteredQuery ()->cursor () as $ row ) {
711721 yield $ row ;
712722 }
713723 };
714724
715- return new FastExcel ($ queryGenerator ($ dataTable ));
725+ return ( new FastExcel ($ queryGenerator ($ dataTable )))-> setColumnStyles ( $ styles );
716726 }
717727
718- return new FastExcel ($ dataTable ->toArray ()['data ' ]);
728+ return ( new FastExcel ($ dataTable ->toArray ()['data ' ]))-> setColumnStyles ( $ styles );
719729 }
720730}
0 commit comments