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,23 @@ 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 || ! $ column ->exportFormat )
712+ ->each (function (Column $ column ) use (&$ styles ) {
713+ $ styles [$ column ->title ] = (new Style )->setFormat ($ column ->exportFormat );
714+ });
715+ 708716 if ($ dataTable instanceof QueryDataTable) {
709717 $ queryGenerator = function ($ dataTable ): Generator {
710718 foreach ($ dataTable ->getFilteredQuery ()->cursor () as $ row ) {
711719 yield $ row ;
712720 }
713721 };
714722
715- return new FastExcel ($ queryGenerator ($ dataTable ));
723+ return ( new FastExcel ($ queryGenerator ($ dataTable )))-> setColumnStyles ( $ styles );
716724 }
717725
718- return new FastExcel ($ dataTable ->toArray ()['data ' ]);
726+ return ( new FastExcel ($ dataTable ->toArray ()['data ' ]))-> setColumnStyles ( $ styles );
719727 }
720728}
0 commit comments