3
3
namespace Yajra \DataTables \Jobs ;
4
4
5
5
use Carbon \Carbon ;
6
+ use DateTimeInterface ;
6
7
use Illuminate \Auth \Events \Login ;
7
8
use Illuminate \Bus \Batchable ;
8
9
use Illuminate \Bus \Queueable ;
20
21
use Illuminate \Support \Facades \Mail ;
21
22
use Illuminate \Support \Facades \Storage ;
22
23
use Illuminate \Support \Str ;
23
- use OpenSpout \Common \Helper \CellTypeHelper ;
24
- use OpenSpout \Common \Type ;
25
- use OpenSpout \Writer \Common \Creator \Style \StyleBuilder ;
26
- use OpenSpout \Writer \Common \Creator \WriterEntityFactory ;
24
+ use OpenSpout \Common \Entity \Cell ;
25
+ use OpenSpout \Common \Entity \Row ;
26
+ use OpenSpout \Common \Entity \Style \Style ;
27
+ use OpenSpout \Writer \Common \Creator \WriterFactory ;
28
+ use OpenSpout \Writer \XLSX \Helper \DateHelper ;
27
29
use OpenSpout \Writer \XLSX \Writer as XLSXWriter ;
28
- use PhpOffice \PhpSpreadsheet \Shared \Date ;
29
30
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
30
31
use Yajra \DataTables \Html \Column ;
31
32
use Yajra \DataTables \Services \DataTable ;
@@ -95,14 +96,14 @@ public function handle()
95
96
$ dataTable = app ()->call ([$ oTable , 'dataTable ' ], compact ('query ' ));
96
97
$ dataTable ->skipPaging ();
97
98
98
- $ exportType = strval (request ('exportType ' ));
99
+ $ exportType = strtolower ( strval (request ('exportType ' ) ));
99
100
100
- $ type = Str::startsWith ($ exportType , Type:: CSV ) ? Type:: CSV : Type:: XLSX ;
101
+ $ type = Str::startsWith ($ exportType , ' csv ' ) ? ' csv ' : ' xlsx ' ;
101
102
$ filename = $ this ->batchId .'. ' .$ type ;
102
103
103
104
$ path = Storage::disk ($ this ->getDisk ())->path ($ filename );
104
105
105
- $ writer = WriterEntityFactory:: createWriter ( $ type );
106
+ $ writer = WriterFactory:: createFromFile ( $ filename );
106
107
$ writer ->openToFile ($ path );
107
108
108
109
if ($ writer instanceof XLSXWriter) {
@@ -112,11 +113,13 @@ public function handle()
112
113
}
113
114
114
115
$ columns = $ this ->getExportableColumns ($ oTable );
115
- $ writer ->addRow (
116
- WriterEntityFactory::createRowFromArray (
117
- $ columns ->map (fn (Column $ column ) => strip_tags ($ column ->title ))->toArray ()
118
- )
119
- );
116
+ $ headers = [];
117
+
118
+ $ columns ->each (function (Column $ column ) use (&$ headers ) {
119
+ $ headers [] = strip_tags ($ column ->title );
120
+ });
121
+
122
+ $ writer ->addRow (Row::fromValues ($ headers ));
120
123
121
124
if ($ this ->usesLazyMethod ()) {
122
125
$ chunkSize = intval (config ('datatables-export.chunk ' , 1000 ));
@@ -144,7 +147,7 @@ public function handle()
144
147
$ property = $ property ['_ ' ] ?? $ column ->name ;
145
148
}
146
149
147
- /** @var array|bool|int|string|null $value */
150
+ /** @var array|bool|int|string|null|DateTimeInterface $value */
148
151
$ value = $ row [$ property ] ?? '' ;
149
152
150
153
if (is_array ($ value )) {
@@ -157,14 +160,14 @@ public function handle()
157
160
$ format = $ column ->exportFormat ?? '@ ' ;
158
161
break ;
159
162
case $ this ->wantsDateFormat ($ column ):
160
- $ cellValue = $ value ? Date:: dateTimeToExcel (Carbon::parse (strval ($ value ))) : '' ;
163
+ $ cellValue = $ value ? DateHelper:: toExcel (Carbon::parse (strval ($ value ))) : '' ;
161
164
$ format = $ column ->exportFormat ?? $ defaultDateFormat ;
162
165
break ;
163
166
case $ this ->wantsNumeric ($ column ):
164
167
$ cellValue = floatval ($ value );
165
168
$ format = $ column ->exportFormat ;
166
169
break ;
167
- case CellTypeHelper:: isDateTimeOrDateInterval ( $ value) :
170
+ case $ value instanceof DateTimeInterface :
168
171
$ cellValue = $ value ;
169
172
$ format = $ column ->exportFormat ?? $ defaultDateFormat ;
170
173
break ;
@@ -173,10 +176,10 @@ public function handle()
173
176
$ format = $ column ->exportFormat ?? NumberFormat::FORMAT_GENERAL ;
174
177
}
175
178
176
- $ cells [] = WriterEntityFactory:: createCell ($ cellValue , (new StyleBuilder )->setFormat ($ format)-> build ( ));
179
+ $ cells [] = Cell:: fromValue ($ cellValue , (new Style )->setFormat ($ format ));
177
180
});
178
181
179
- $ writer ->addRow (WriterEntityFactory:: createRow ($ cells ));
182
+ $ writer ->addRow (new Row ($ cells ));
180
183
}
181
184
182
185
$ writer ->close ();
@@ -199,14 +202,6 @@ protected function getDisk(): string
199
202
return strval (config ('datatables-export.disk ' , 'local ' ));
200
203
}
201
204
202
- /**
203
- * @return string
204
- */
205
- protected function getS3Disk (): string
206
- {
207
- return strval (config ('datatables-export.s3_disk ' , '' ));
208
- }
209
-
210
205
/**
211
206
* @param \Yajra\DataTables\Services\DataTable $dataTable
212
207
* @return \Illuminate\Support\Collection<array-key, Column>
@@ -279,15 +274,23 @@ protected function isNumeric($value): bool
279
274
}
280
275
281
276
/**
282
- * @param array $data
277
+ * @return string
278
+ */
279
+ protected function getS3Disk (): string
280
+ {
281
+ return strval (config ('datatables-export.s3_disk ' , '' ));
282
+ }
283
+
284
+ /**
285
+ * @param array $data
283
286
* @return void
284
287
*/
285
288
public function sendResults (array $ data ): void
286
289
{
287
290
Mail::send ('datatables-export::export-email ' , $ data , function ($ message ) use ($ data ) {
288
291
$ message ->attach ($ data ['path ' ]);
289
292
$ message ->to ($ data ['email ' ])
290
- ->subject ('Export Report ' );
293
+ ->subject ('Export Report ' );
291
294
$ message ->from (config ('datatables-export.mail_from ' ));
292
295
});
293
296
}
0 commit comments