【问题标题】:Errorr Returnn value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, string returnedMaatwebsite\Excel\Sheet::mapArraybleRow() 的错误返回值必须是数组类型,返回字符串
【发布时间】:2020-05-06 13:04:46
【问题描述】:

我刚刚将 Laravel 5.4 升级到 5.5,现在我必须更改所有使用旧 Laravel-Excel 的编码。

我使用的是 php 7.2.25,Windows/Wamp。

我正在尝试上传一个 excel 文件,获取它的数据,对其进行大量检查和计算(尚未在代码中),然后创建一个新的 excel 文件并为用户提供 Windows 的“保存文件”选项。

  1. 不确定如何获取 Windows 保存文件窗口,文档上没有任何说明。

    1. 如果我无法获得 Windows 保存文件窗口,我不确定如何将路径设置为:例如 My Documents\test。
  2. 使用我的代码,我得到了这个错误:

    Symfony\Component\Debug\Exception\FatalThrowableError (E_RECOVERABLE_ERROR) 类型错误:Maatwebsite\Excel\Sheet::mapArraybleRow()的返回值必须是数组类型,返回字符串

我的导入类代码:

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');

我的导出类:

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;
    }
}

有人可以帮忙吗?

【问题讨论】:

    标签: php laravel laravel-5 maatwebsite-excel


    【解决方案1】:
    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);  
    

    决定不增加额外的框架。 Laravel 7*,Maatwebsite 3*

    【讨论】:

      【解决方案2】:

      一天半之后,我设法让它工作了。

      工作代码:

      导入类:

      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;
          }
      }
      

      导出类:

      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;
          }
      }
      

      我的控制器:

      public function importTimesheets(Request $request)
          {        
              $import = new TimesheetsImport;
              $rows = Excel::toCollection($import, $request->file('file'));
      
              return Excel::download(new TimesheetsExport($rows), 'test.xlsx');
          }
      

      使用此代码,我也获得了 Windows 的“保存文件”。

      这不好玩,但是已经完成了,希望对其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-10-25
        • 2018-07-24
        • 2017-04-22
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        • 2019-03-23
        相关资源
        最近更新 更多