29
29
namespace Zuko \Flex2Cell \Traits ;
30
30
31
31
32
- use Illuminate \Database \Eloquent \Model ;
33
- use Illuminate \Support \Collection ;
34
32
use PhpOffice \PhpSpreadsheet \Cell \Coordinate ;
35
33
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
36
34
use PhpOffice \PhpSpreadsheet \Writer \Xls ;
@@ -220,7 +218,9 @@ public function export(string $filename)
220
218
$ this ->writeHeaders ($ sheet );
221
219
}
222
220
$ rowIndex = $ this ->appendMode ? $ sheet ->getHighestRow () + 1 : 3 ; // Start from row 3 due to double header
223
- if ($ this ->data instanceof Collection || $ this ->data instanceof Model) {
221
+ /** @noinspection PhpUndefinedClassInspection */
222
+ /** @noinspection PhpUndefinedNamespaceInspection */
223
+ if ($ this ->data instanceof \Illuminate \Support \Collection || $ this ->data instanceof \Illuminate \Database \Eloquent \Model) {
224
224
$ this ->data = $ this ->data ->toArray ();
225
225
}
226
226
foreach (array_chunk ($ this ->data , $ this ->chunkSize ) as $ chunk ) {
@@ -259,12 +259,15 @@ public function export(string $filename)
259
259
*/
260
260
protected function writeHeaders ($ sheet )
261
261
{
262
+ $ this ->headerRowIndex = 1 ; // Start with assuming headers are in the first row
262
263
$ columnIndex = 1 ;
263
264
foreach ($ this ->headers as $ header ) {
264
265
if (!in_array ($ header , $ this ->hiddens , true )) {
265
- $ sheet ->setCellValue ([$ columnIndex , 1 ], $ this ->getHeader ($ header ));
266
+ $ displayHeader = $ this ->getHeader ($ header );
267
+ $ sheet ->setCellValue ([$ columnIndex , $ this ->headerRowIndex ], $ displayHeader );
266
268
if (isset ($ this ->subHeaders [$ header ])) {
267
- $ sheet ->setCellValue ([$ columnIndex , 2 ], $ this ->getSubHeader ($ header ));
269
+ $ sheet ->setCellValue ([$ columnIndex , $ this ->headerRowIndex + 1 ], $ this ->getSubHeader ($ header ));
270
+ $ this ->headerRowIndex = 2 ; // If we have subheaders, main headers are now in row 2
268
271
}
269
272
$ columnIndex ++;
270
273
}
@@ -428,7 +431,7 @@ protected function getColumnLetter($mappingKey)
428
431
*
429
432
* @param string $header The header
430
433
*
431
- * @return string The mapping key if found, null otherwise
434
+ * @return string|null The mapping key if found, null otherwise
432
435
*/
433
436
protected function getMappingKeyFromHeader ($ header )
434
437
{
@@ -440,7 +443,7 @@ protected function getMappingKeyFromHeader($header)
440
443
*
441
444
* @param string $mappingKey The mapping key
442
445
*
443
- * @return string The header if found, null otherwise
446
+ * @return string|null The header if found, null otherwise
444
447
*/
445
448
protected function getHeaderFromMappingKey ($ mappingKey )
446
449
{
@@ -450,7 +453,7 @@ protected function getHeaderFromMappingKey($mappingKey)
450
453
private static function first (array $ array , $ callback = null , $ default = null ) {
451
454
if (is_null ($ callback )) {
452
455
if (empty ($ array )) {
453
- return value ( $ default) ;
456
+ return $ default ;
454
457
}
455
458
456
459
foreach ($ array as $ item ) {
@@ -464,6 +467,6 @@ private static function first(array $array, $callback = null, $default = null) {
464
467
}
465
468
}
466
469
467
- return value ( $ default) ;
470
+ return $ default ;
468
471
}
469
472
}
0 commit comments