Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef3d3f2

Browse files
committed
feat: export cell styling via exportFormat
1 parent 1765963 commit ef3d3f2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

‎src/Services/DataTable.php‎

Lines changed: 17 additions & 7 deletions
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,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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /