22
33namespace Yajra \DataTables \Exports ;
44
5+ use Carbon \Carbon ;
56use Illuminate \Support \Collection ;
67use Maatwebsite \Excel \Concerns \Exportable ;
78use Maatwebsite \Excel \Concerns \FromQuery ;
9+ use Maatwebsite \Excel \Concerns \WithColumnFormatting ;
810use Maatwebsite \Excel \Concerns \WithHeadings ;
911use Maatwebsite \Excel \Concerns \WithMapping ;
12+ use PhpOffice \PhpSpreadsheet \Shared \Date ;
13+ use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
1014use Yajra \DataTables \Html \Column ;
1115
12- class DataTableQueuedExport implements FromQuery, WithMapping, WithHeadings
16+ class DataTableQueuedExport implements FromQuery, WithMapping, WithHeadings, WithColumnFormatting
1317{
1418 use Exportable;
1519
1620 protected $ query ;
1721 protected $ columns ;
1822
23+ /**
24+ * Index of fields with date instance.
25+ *
26+ * @var array
27+ */
28+ protected $ dates = [];
29+
1930 public function __construct ($ query , Collection $ columns )
2031 {
2132 $ this ->query = $ query ;
@@ -30,8 +41,24 @@ public function query()
3041 public function map ($ row ): array
3142 {
3243 return $ this ->columns
33- ->map (function (Column $ column ) use ($ row ) {
34- return $ row [$ column ['data ' ]];
44+ ->map (function (Column $ column , $ index ) use ($ row ) {
45+ $ property = $ column ['data ' ];
46+
47+ if ($ row [$ property ] instanceof \DateTime) {
48+ $ this ->dates [] = $ index ;
49+
50+ return Date::dateTimeToExcel ($ row [$ property ]);
51+ }
52+
53+ if ($ this ->wantsDateFormat ($ column )) {
54+ $ this ->dates [] = $ index ;
55+
56+ $ dateValue = $ row [$ property ];
57+
58+ return $ dateValue ? Date::dateTimeToExcel (Carbon::parse ($ dateValue )) : '' ;
59+ }
60+
61+ return $ row [$ property ];
3562 })
3663 ->toArray ();
3764 }
@@ -40,4 +67,45 @@ public function headings(): array
4067 {
4168 return $ this ->columns ->pluck ('title ' )->toArray ();
4269 }
70+
71+ public function columnFormats (): array
72+ {
73+ $ formats = [];
74+
75+ $ this ->columns
76+ ->each (function (Column $ column , $ index ) use (&$ formats ) {
77+ if (in_array ($ index , $ this ->dates ) || $ this ->wantsDateFormat ($ column )) {
78+ return $ formats [$ this ->num2alpha ($ index - 1 )] = $ column ['exportFormat ' ] ?? NumberFormat::FORMAT_DATE_YYYYMMDD ;
79+ }
80+
81+ if (isset ($ column ['exportFormat ' ])) {
82+ return $ formats [$ this ->num2alpha ($ index - 1 )] = $ column ['exportFormat ' ];
83+ }
84+ })
85+ ->toArray ();
86+
87+ return $ formats ;
88+ }
89+
90+ protected function num2alpha ($ n )
91+ {
92+ for ($ r = "" ; $ n >= 0 ; $ n = intval ($ n / 26 ) - 1 ) {
93+ $ r = chr ($ n % 26 + 0x41 ) . $ r ;
94+ }
95+
96+ return $ r ;
97+ }
98+
99+ /**
100+ * @param \Yajra\DataTables\Html\Column $column
101+ * @return bool
102+ */
103+ protected function wantsDateFormat (Column $ column ): bool
104+ {
105+ if (!isset ($ column ['exportFormat ' ])) {
106+ return false ;
107+ }
108+
109+ return in_array ($ column ['exportFormat ' ], config ('datatables-export.date_formats ' , []));
110+ }
43111}
0 commit comments