Skip to content

Commit 10071e0

Browse files
authored
Merge pull request #13 from yajra/spout
Use box/spout instead of Laravel Excel.
2 parents 62634db + 4fe3d4c commit 10071e0

File tree

4 files changed

+74
-119
lines changed

4 files changed

+74
-119
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"php": ">=7.4|8.*",
2121
"yajra/laravel-datatables-buttons": "4.*",
2222
"yajra/laravel-datatables-html": "^4.40",
23-
"livewire/livewire": "2.*"
23+
"livewire/livewire": "2.*",
24+
"box/spout": "^3.3"
2425
},
2526
"require-dev": {
2627
"phpunit/phpunit": "^9.5.9"

src/Exports/DataTableQueuedExport.php

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/Jobs/DataTableExportJob.php

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Yajra\DataTables\Jobs;
44

5+
use Box\Spout\Common\Type;
6+
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
7+
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
8+
use Carbon\Carbon;
59
use Illuminate\Bus\Batchable;
610
use Illuminate\Bus\Queueable;
711
use Illuminate\Contracts\Queue\ShouldBeUnique;
@@ -11,7 +15,8 @@
1115
use Illuminate\Queue\SerializesModels;
1216
use Illuminate\Support\Facades\Auth;
1317
use Illuminate\Support\Str;
14-
use Yajra\DataTables\Exports\DataTableQueuedExport;
18+
use PhpOffice\PhpSpreadsheet\Shared\Date;
19+
use Yajra\DataTables\Html\Column;
1520
use Yajra\DataTables\Services\DataTable;
1621

1722
class DataTableExportJob implements ShouldQueue, ShouldBeUnique
@@ -63,12 +68,67 @@ public function handle()
6368
$dataTable = app()->call([$oTable, 'dataTable'], compact('query'));
6469
$dataTable->skipPaging();
6570

66-
$type = Str::startsWith(request('exportType'), 'csv') ? '.csv' : '.xlsx';
67-
$path = 'exports/'.$this->batchId.$type;
71+
$type = Str::startsWith(request('exportType'), Type::CSV) ? Type::CSV : Type::XLSX;
72+
$writer = WriterEntityFactory::createWriter($type);
73+
$writer->openToFile(storage_path('app/exports/' . $this->batchId . '.' . $type));
6874

69-
(new DataTableQueuedExport(
70-
$dataTable->getFilteredQuery(),
71-
$oTable->html()->getColumns()->filter->exportable
72-
))->store($path);
75+
$columns = $oTable->html()->getColumns()->filter->exportable;
76+
$writer->addRow(WriterEntityFactory::createRowFromArray($columns->pluck('title')->toArray()));
77+
78+
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
79+
$cells = collect();
80+
$columns->map(function (Column $column, $index) use ($row, $cells) {
81+
$property = $column['data'];
82+
$value = $row->{$property} ?? '';
83+
84+
if ($value instanceof \DateTime || $this->wantsDateFormat($column)) {
85+
$date = $value ? Date::dateTimeToExcel(Carbon::parse($value)) : '';
86+
$defaultDateFormat = config('datatables-export.default_date_format', 'yyyy-mm-dd');
87+
$format = $column['exportFormat'] ?? $defaultDateFormat;
88+
89+
$cells->push(
90+
WriterEntityFactory::createCell($date, (new StyleBuilder)->setFormat($format)->build())
91+
);
92+
} else {
93+
$format = $column['exportFormat']
94+
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
95+
: null;
96+
97+
$value = $this->isNumeric($value) ? (float) $value : $value;
98+
99+
$cells->push(WriterEntityFactory::createCell($value, $format));
100+
}
101+
});
102+
103+
$writer->addRow(WriterEntityFactory::createRow($cells->toArray()));
104+
}
105+
$writer->close();
106+
}
107+
108+
/**
109+
* @param \Yajra\DataTables\Html\Column $column
110+
* @return bool
111+
*/
112+
protected function wantsDateFormat(Column $column): bool
113+
{
114+
if (!isset($column['exportFormat'])) {
115+
return false;
116+
}
117+
118+
return in_array($column['exportFormat'], config('datatables-export.date_formats', []));
119+
}
120+
121+
/**
122+
* @param mixed $value
123+
* @return bool
124+
*/
125+
protected function isNumeric($value): bool
126+
{
127+
// Skip numeric style if value has leading zeroes.
128+
if (Str::startsWith($value, '0')) {
129+
return false;
130+
}
131+
132+
return is_numeric($value);
73133
}
74134
}

src/config/datatables-export.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
return [
66

7+
/**
8+
* Default export format for date.
9+
*/
10+
'default_date_format' => 'yyyy-mm-dd',
11+
712
/**
813
* List of valid date formats to be used for auto-detection.
914
*/

0 commit comments

Comments
 (0)