Skip to content

Commit df2660d

Browse files
committed
Add support for fast-excel for exporting with xls and csv.
1 parent d9bcd72 commit df2660d

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

src/Services/DataTable.php

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Illuminate\Http\JsonResponse;
66
use Illuminate\Support\Collection;
7+
use Rap2hpoutre\FastExcel\FastExcel;
78
use Yajra\DataTables\Contracts\DataTableButtons;
89
use Yajra\DataTables\Contracts\DataTableScope;
910
use Yajra\DataTables\Html\Column;
11+
use Yajra\DataTables\QueryDataTable;
1012
use Yajra\DataTables\Transformers\DataArrayTransformer;
1113

1214
abstract class DataTable implements DataTableButtons
@@ -116,6 +118,13 @@ abstract class DataTable implements DataTableButtons
116118
*/
117119
protected $request;
118120

121+
/**
122+
* Flag to use fast-excel package for export.
123+
*
124+
* @var bool
125+
*/
126+
protected $fastExcel = false;
127+
119128
/**
120129
* Export class handler.
121130
*
@@ -395,7 +404,7 @@ public function excel()
395404
{
396405
$ext = '.' . strtolower($this->excelWriter);
397406

398-
return $this->buildExcelFile()->download($this->getFilename() . $ext, $this->excelWriter);
407+
return $this->buildExcelFile()->download($this->getFilename() . $ext);
399408
}
400409

401410
/**
@@ -405,17 +414,39 @@ public function excel()
405414
*/
406415
protected function buildExcelFile()
407416
{
408-
if ($this->exportClass != DataTablesExportHandler::class) {
409-
$collection = collect($this->getAjaxResponseData());
410-
} else {
411-
$collection = collect($this->getDataForExport());
417+
if ($this->fastExcel) {
418+
$query = null;
419+
if (method_exists($this, 'query')) {
420+
$query = app()->call([$this, 'query']);
421+
$query = $this->applyScopes($query);
422+
}
423+
424+
/** @var \Yajra\DataTables\DataTableAbstract $dataTable */
425+
$dataTable = app()->call([$this, 'dataTable'], compact('query'));
426+
$dataTable->skipPaging();
427+
428+
if ($dataTable instanceof QueryDataTable) {
429+
function queryGenerator($dataTable) {
430+
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
431+
yield $row;
432+
}
433+
}
434+
435+
return new FastExcel(queryGenerator($dataTable));
436+
}
437+
438+
return new FastExcel($this->convertToLazyCollection($dataTable->toArray()['data']));
412439
}
413440

414-
if (method_exists($collection, 'lazy')) {
415-
$collection->lazy();
441+
if ($this->exportClass != DataTablesExportHandler::class) {
442+
$collection = $this->getAjaxResponseData();
443+
444+
return new $this->exportClass($this->convertToLazyCollection($collection));
416445
}
417446

418-
return new $this->exportClass($collection);
447+
$collection = $this->getDataForExport();
448+
449+
return new $this->exportClass($this->convertToLazyCollection($collection));
419450
}
420451

421452
/**
@@ -506,7 +537,7 @@ public function csv()
506537
{
507538
$ext = '.' . strtolower($this->csvWriter);
508539

509-
return $this->buildExcelFile()->download($this->getFilename() . $ext, $this->csvWriter);
540+
return $this->buildExcelFile()->download($this->getFilename() . $ext);
510541
}
511542

512543
/**
@@ -637,4 +668,21 @@ protected function getBuilderParameters()
637668
{
638669
return config('datatables-buttons.parameters');
639670
}
671+
672+
/**
673+
* @param \Illuminate\Support\|array $collection
674+
* @return \Illuminate\Support\Collection
675+
*/
676+
protected function convertToLazyCollection($collection)
677+
{
678+
if (is_array($collection)) {
679+
$collection = collect($collection);
680+
}
681+
682+
if (method_exists($collection, 'lazy')) {
683+
$collection->lazy();
684+
}
685+
686+
return $collection;
687+
}
640688
}

0 commit comments

Comments
 (0)