|
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,67 @@ 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 | + $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); |
73 | 133 | }
|
74 | 134 | }
|
0 commit comments