Skip to content

Commit 44d7ada

Browse files
authored
Merge pull request #186 from yajra/patch-1
feat: fast-excel export cell styling via exportFormat
2 parents 1765963 + 84f224a commit 44d7ada

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/Services/DataTable.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Support\Collection;
1616
use Illuminate\Support\LazyCollection;
1717
use Maatwebsite\Excel\ExcelServiceProvider;
18+
use OpenSpout\Common\Entity\Style\Style;
1819
use Rap2hpoutre\FastExcel\FastExcel;
1920
use Yajra\DataTables\Contracts\DataTableButtons;
2021
use 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

Comments
 (0)