33namespace Yajra \DataTables \Jobs ;
44
55use Carbon \Carbon ;
6+ use DateTimeInterface ;
67use Illuminate \Auth \Events \Login ;
78use Illuminate \Bus \Batchable ;
89use Illuminate \Bus \Queueable ;
2021use Illuminate \Support \Facades \Mail ;
2122use Illuminate \Support \Facades \Storage ;
2223use 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 ;
2729use OpenSpout \Writer \XLSX \Writer as XLSXWriter ;
28- use PhpOffice \PhpSpreadsheet \Shared \Date ;
2930use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
3031use Yajra \DataTables \Html \Column ;
3132use Yajra \DataTables \Services \DataTable ;
@@ -95,14 +96,14 @@ public function handle()
9596 $ dataTable = app ()->call ([$ oTable , 'dataTable ' ], compact ('query ' ));
9697 $ dataTable ->skipPaging ();
9798
98- $ exportType = strval (request ('exportType ' ));
99+ $ exportType = strtolower ( strval (request ('exportType ' ) ));
99100
100- $ type = Str::startsWith ($ exportType , Type:: CSV ) ? Type:: CSV : Type:: XLSX ;
101+ $ type = Str::startsWith ($ exportType , ' csv ' ) ? ' csv ' : ' xlsx ' ;
101102 $ filename = $ this ->batchId .'. ' .$ type ;
102103
103104 $ path = Storage::disk ($ this ->getDisk ())->path ($ filename );
104105
105- $ writer = WriterEntityFactory:: createWriter ( $ type );
106+ $ writer = WriterFactory:: createFromFile ( $ filename );
106107 $ writer ->openToFile ($ path );
107108
108109 if ($ writer instanceof XLSXWriter) {
@@ -112,11 +113,13 @@ public function handle()
112113 }
113114
114115 $ 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 ));
120123
121124 if ($ this ->usesLazyMethod ()) {
122125 $ chunkSize = intval (config ('datatables-export.chunk ' , 1000 ));
@@ -144,7 +147,7 @@ public function handle()
144147 $ property = $ property ['_ ' ] ?? $ column ->name ;
145148 }
146149
147- /** @var array|bool|int|string|null $value */
150+ /** @var array|bool|int|string|null|DateTimeInterface $value */
148151 $ value = $ row [$ property ] ?? '' ;
149152
150153 if (is_array ($ value )) {
@@ -157,14 +160,14 @@ public function handle()
157160 $ format = $ column ->exportFormat ?? '@ ' ;
158161 break ;
159162 case $ this ->wantsDateFormat ($ column ):
160- $ cellValue = $ value ? Date:: dateTimeToExcel (Carbon::parse (strval ($ value ))) : '' ;
163+ $ cellValue = $ value ? DateHelper:: toExcel (Carbon::parse (strval ($ value ))) : '' ;
161164 $ format = $ column ->exportFormat ?? $ defaultDateFormat ;
162165 break ;
163166 case $ this ->wantsNumeric ($ column ):
164167 $ cellValue = floatval ($ value );
165168 $ format = $ column ->exportFormat ;
166169 break ;
167- case CellTypeHelper:: isDateTimeOrDateInterval ( $ value) :
170+ case $ value instanceof DateTimeInterface :
168171 $ cellValue = $ value ;
169172 $ format = $ column ->exportFormat ?? $ defaultDateFormat ;
170173 break ;
@@ -173,10 +176,10 @@ public function handle()
173176 $ format = $ column ->exportFormat ?? NumberFormat::FORMAT_GENERAL ;
174177 }
175178
176- $ cells [] = WriterEntityFactory:: createCell ($ cellValue , (new StyleBuilder )->setFormat ($ format)-> build ( ));
179+ $ cells [] = Cell:: fromValue ($ cellValue , (new Style )->setFormat ($ format ));
177180 });
178181
179- $ writer ->addRow (WriterEntityFactory:: createRow ($ cells ));
182+ $ writer ->addRow (new Row ($ cells ));
180183 }
181184
182185 $ writer ->close ();
@@ -199,14 +202,6 @@ protected function getDisk(): string
199202 return strval (config ('datatables-export.disk ' , 'local ' ));
200203 }
201204
202- /**
203- * @return string
204- */
205- protected function getS3Disk (): string
206- {
207- return strval (config ('datatables-export.s3_disk ' , '' ));
208- }
209-
210205 /**
211206 * @param \Yajra\DataTables\Services\DataTable $dataTable
212207 * @return \Illuminate\Support\Collection<array-key, Column>
@@ -279,15 +274,23 @@ protected function isNumeric($value): bool
279274 }
280275
281276 /**
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
283286 * @return void
284287 */
285288 public function sendResults (array $ data ): void
286289 {
287290 Mail::send ('datatables-export::export-email ' , $ data , function ($ message ) use ($ data ) {
288291 $ message ->attach ($ data ['path ' ]);
289292 $ message ->to ($ data ['email ' ])
290- ->subject ('Export Report ' );
293+ ->subject ('Export Report ' );
291294 $ message ->from (config ('datatables-export.mail_from ' ));
292295 });
293296 }
0 commit comments