Skip to content

Commit d930ad8

Browse files
Merge branch 'yajra:master' into master
2 parents c014e2f + 6e12133 commit d930ad8

File tree

6 files changed

+91
-120
lines changed

6 files changed

+91
-120
lines changed

CHANGELOG.md

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

33
## UNRELEASED
44

5+
## v0.6.0 - 10-13-2021
6+
7+
- Use box/spout instead of Laravel Excel. #13
8+
- Add support for number format and text with leading zeroes.
9+
510
## v0.5.2 - 10-13-2021
611

712
- Fix export when used with Eloquent Builder. #12

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
[![Total Downloads](https://img.shields.io/packagist/dt/yajra/laravel-datatables-export.svg)](https://packagist.org/packages/yajra/laravel-datatables-export)
77
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/yajra/laravel-datatables-export)
88

9-
This package is a plugin of [Laravel DataTables](https://github.com/yajra/laravel-datatables) for handling server-side exporting using Queue, Laravel Excel and Livewire.
9+
This package is a plugin of [Laravel DataTables](https://github.com/yajra/laravel-datatables) for handling server-side exporting using Queue, Spout and Livewire.
1010

1111
## Requirements
1212

1313
- [PHP >=7.4](http://php.net/)
1414
- [Laravel 8.x](https://github.com/laravel/framework)
1515
- [Laravel Livewire](https://laravel-livewire.com/)
16+
- [Spout](https://github.com/box/spout)
1617
- [Laravel DataTables 9.x](https://github.com/yajra/laravel-datatables)
1718
- [jQuery DataTables v1.10.x](http://datatables.net/)
1819

@@ -96,6 +97,16 @@ Column::make('mobile')->exportFormat('00000000000'),
9697

9798
The format above will treat mobile numbers as text with leading zeroes.
9899

100+
## Numeric Fields Formatting
101+
102+
The package will auto-detect numeric fields and can be used with custom formats.
103+
104+
```phpt
105+
Column::make('total')->exportFormat('0.00'),
106+
Column::make('count')->exportFormat('#,##0'),
107+
Column::make('average')->exportFormat('#,##0.00),
108+
```
109+
99110
## Date Fields Formatting
100111

101112
The package will auto-detect date fields when used with a valid format or is a DateTime instance.

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)