简体   繁体   中英

Errorr Returnn value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, string returned

I just upgraded Laravel 5.4 to 5.5 and now I have to change all the coding that used the old Laravel-Excel.

I'm using php 7.2.25, Windows/Wamp.

I am trying to upload an excel file, get it's data, do lots of check and calculations on it (Not in the code yet) and then create a new excel file and give the user the Windows 'Save File' option.

  1. Not sure how to get the Windows save file window, don't see any explanation on the documentation.

    1. If I can't get the Windows save file window, I'm not sure how to set the path to: My Documents\\test for example.
  2. With the code I have, I get this error:

    Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError (E_RECOVERABLE_ERROR) Type error: Return value of Maatwebsite\\Excel\\Sheet::mapArraybleRow() must be of the type array, string returned

My Import class code:

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Exports\TimesheetsExport;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Facades\Excel; 
use App\User;

class TimesheetsImport implements ToCollection, WithHeadingRow
{
private $user;

public function __construct($param)
{
    $this->user = $param;
}

public function collection(Collection $rows)
{
    $data = new Collection([$this->user->fullName()]);
    foreach ($rows as $row) 
    {
        $data->put($row['date'], $row['in'], $row['out']);
    }

return Excel::download(new TimesheetsExport($data), 'testtttt.xlsx');

My Export class:

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;

class TimesheetsExport implements FromCollection
{
    protected $rows;

    public function __construct(Collection $rows)
    {
        $this->rows = $rows;
    }
    
    public function collection()
    {
        return $this->rows;
    }
}

Can someone please help?

After a day and a half, I managed to make it work.

The working code:

Import Class:

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Facades\Excel;
use App\User;

class TimesheetsImport implements ToCollection, WithHeadingRow
{
    public $data;

    public function collection($rows)
    {
        $this->data = $rows;
    }
}

Export class:

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;

class TimesheetsExport implements FromCollection
{
    protected $rows;

    public function __construct($rows)
    {
        $this->rows = $rows;
    }

    public function collection()
    {
        return $this->rows;
    }
}

My controller:

public function importTimesheets(Request $request)
    {        
        $import = new TimesheetsImport;
        $rows = Excel::toCollection($import, $request->file('file'));

        return Excel::download(new TimesheetsExport($rows), 'test.xlsx');
    }

With this code, I get the Windows 'Save File' as well.

This was not fun, but it's done, I hope it will help someone else.

use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Excel;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Excel as ExcelType; 

...

$array = [[1,2,3],[3,2,1]];

return  \Excel::download(new class($array) implements FromArray{ 
            public function __construct($array)
            {
                $this->array = $array;
            }
            public function array(): array
            {
                return $this->array;
            }
        },'db.xlsx', ExcelType::XLSX);  

decided so without additional frames. Laravel 7*, Maatwebsite 3*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM