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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49473098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:45:42  来源:igfitidea点击:

Call to undefined method Maatwebsite\Excel\Excel::load()

phpexcellaravellaravel-5maatwebsite-excel

提问by Rio Aditya

I'm trying to import excel file (.xlsx) using maatwebsite 3.0. How to fix This error

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

Call to undefined method Maatwebsite\Excel\Excel::load()

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

My controller

我的控制器

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

回答by jedrzej.kurylo

Version 3.0of that package doesn't handle imports yet. Release date for this feature is unknown. See this post for more details: https://medium.com/@maatwebsite/laravel-excel-lessons-learned-7fee2812551

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

I suggest you switch to version 2.*.

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

回答by Dawood Iddris

Hi there in version 3 the load method was remove so switch back to version two like so try using this command,

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

composer require "maatwebsite/excel:~2.1.0"

composer require "maatwebsite/excel:~2.1.0"

回答by Aravind

^3.0versions of maatwebsite/exceldoes not support loads.
Delete the config/excel.phpfile first .

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

Downgrade the maatwebsite/excelversion by changing your composer.jsonfile from "maatwebsite/excel": "^3.1", to "maatwebsite/excel": "~2.1.0", and performing 'composer update'.

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

And if you get an error:

如果出现错误:

Undefined class constant 'XLSX' in 2.1 version

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

this is what you have to do.
Delete the config/excel.php before downgrading then perform the composer update.

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

回答by Engr Syed Rowshan Ali

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 .

所有 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)

3.0 provides no convenience methods for styling, you are encouraged to use PhpSpreadsheets native methods.

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

回答by rap-2-h

Version 3.0 of Laravel Excel doesn't handle imports.

Laravel Excel 3.0 版不处理导入。

You could also use an alternative package that works with import such as:

您还可以使用与导入一起使用的替代包,例如:

Both handles import.

两者都处理导入。

You could also switch to version 2, but it means use an old version of a lib.

您也可以切换到版本 2,但这意味着使用旧版本的 lib。