调用未定义的方法Maatwebsite\Excel\Excel :: load()

Rio*_*tya 8 php excel laravel laravel-5 maatwebsite-excel

我正在尝试使用maatwebsite 3.0导入excel文件(.xlsx).如何修复此错误

调用未定义的方法Maatwebsite\Excel\Excel :: load()

我的控制器

public function importsave(Request $request)
{
   if($request->hasFile('excel'))
    {
        $path = $request->file('excel')->getRealPath();
        $data= Excel::load($path, function($reader) {})->get();
        if(!empty($data) && $data->count())
        {
            foreach($data->toArray() as $key=>$value)
            {
                if(!empty($value))
                {
                    Employee::insert($value);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

jed*_*ylo 20

该软件包的3.0尚未处理导入.此功能的发布日期未知.有关详细信息,请参阅此帖子:https://medium.com/@maatwebsite/laravel-excel-lessons-learned-7fee2812551

我建议你切换到版本2.*.


Eng*_*Ali 11

所有 Laravel Excel 2.* 方法均已弃用,无法在 3.0 中使用。

Excel::load() is removed and replaced by Excel::import($yourImport)
Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
Excel::create()->string('xlsx') is removed an replaced by Excel::raw($yourExport, Excel::XLSX)
Run Code Online (Sandbox Code Playgroud)

3.0 没有提供方便的样式方法,鼓励您使用 PhpSpreadsheets 本地方法。


小智 11

不要惊慌:)就这样做

Excel::toArray([],$filePath);
Run Code Online (Sandbox Code Playgroud)

只需传递一个空数组作为第一个参数


Ara*_*ind 7

^3.0的版本maatwebsite/excel不支持负载。
先删除config/excel.php文件。

maatwebsite/excel通过将composer.json文件从 "maatwebsite/excel": "^3.1"、更改为"maatwebsite/excel": "~2.1.0"并执行“作曲家更新”来 降级版本。

如果出现错误:

2.1 版本中未定义的类常量“XLSX”

这是你必须做的。
在降级之前删除 config/excel.php 然后执行 Composer 更新。


小智 6

嗨,在版本3中,加载方法已删除,因此请切换回版本2,因此请尝试使用此命令,

composer require "maatwebsite/excel:~2.1.0"