|
2 | 2 |
|
3 | 3 | namespace Yajra\DataTables\Jobs;
|
4 | 4 |
|
| 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; |
5 | 9 | use Illuminate\Bus\Batchable;
|
6 | 10 | use Illuminate\Bus\Queueable;
|
7 | 11 | use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
11 | 15 | use Illuminate\Queue\SerializesModels;
|
12 | 16 | use Illuminate\Support\Facades\Auth;
|
13 | 17 | use Illuminate\Support\Str;
|
14 |
| -use Yajra\DataTables\Exports\DataTableQueuedExport; |
| 18 | +use PhpOffice\PhpSpreadsheet\Shared\Date; |
| 19 | +use Yajra\DataTables\Html\Column; |
15 | 20 | use Yajra\DataTables\Services\DataTable;
|
16 | 21 |
|
17 | 22 | class DataTableExportJob implements ShouldQueue, ShouldBeUnique
|
@@ -63,12 +68,51 @@ public function handle()
|
63 | 68 | $dataTable = app()->call([$oTable, 'dataTable'], compact('query'));
|
64 | 69 | $dataTable->skipPaging();
|
65 | 70 |
|
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)); |
68 | 74 |
|
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 | + $cells->push(WriterEntityFactory::createCell($value, $format)); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + $writer->addRow(WriterEntityFactory::createRow($cells->toArray())); |
| 102 | + } |
| 103 | + $writer->close(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param \Yajra\DataTables\Html\Column $column |
| 108 | + * @return bool |
| 109 | + */ |
| 110 | + protected function wantsDateFormat(Column $column): bool |
| 111 | + { |
| 112 | + if (!isset($column['exportFormat'])) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return in_array($column['exportFormat'], config('datatables-export.date_formats', [])); |
73 | 117 | }
|
74 | 118 | }
|
0 commit comments