先确保有phpexcel插件 composer require phpoffice/phpexcel

1.添加路由至 routes.php

2.新建控制器

namespace App\Admin\Controllers;

use App\Admin\Extensions\GlobalUploadButton;
use App\Models\Admin\Cpyifmct;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class ExporterController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = 'Example controller';

/**
 * Make a grid builder.
 *
 * @return Grid
 */
protected function grid()
{
    $grid = new Grid(new Cpyifmct);

    //在控制器的grid方法里面加入按钮
    $grid->tools(function ($tools) {
        $tools->append(new GlobalUploadButton());
    });
    return $grid;
}

public function upload(Request $request)
{
    //获得请求的方法
    $method = $request->method();

    //post请求则解析文件
    if ($method == 'POST'){

        //按时间命名文件
        $name = time().'.xls';

        //文件保存到storage/app/public
        $path = $request->file('upfile')->storeAs('public',$name);

        //获取文件url
        $url = storage_path().'/app/'.$path;

        //读取文件
        $file = \PHPExcel_IOFactory::load($url);

        //文件转数组
        $re = $file->getSheet(0)->toArray(null,false,false,true);

        //初始化下标0到数组(excel没有下标为0的数组,从1开始),避免后面读取报错
        $re[0] = '';

        //写入字段
        $header = ['field1','field2','field3'];

        //获取文件数组条目数
        $recounts = count($re);

        //开始循环写入数据库,为什么$i=2?,因为第一行为标头
        for ($i=2;$i<$recounts;$i++){
            //重置数组
            $e = NUll;
            $insert[] = [];
            $arr[] = [];
            $f = 0;
            //循环写入数组
            foreach($re[$i] as $r){
                //最大数组数为字段数
                if ($f < count($header)){
                    //你的代码。。。balabala
                    //组装数据
                    $arr[$f] = [
                        $header[$f] => $r,
                    ];
                    $f++;
                }

            }

            //初始化集合
            $insert = collect($arr);

            //转为一维集合
            $e = $insert->collapse();

            //转换为数组
            $ee = $e->toArray();

            //写入数据库
            DB::table('table')->insert([$ee]);
        }

        //提示,当然,可以自己用try来处理数据库异常,这里懒得处理了。。。
        $success = new MessageBag([
            'title'   => '恭喜',
            'message' => '导入成功',
        ]);

        return back()->with(compact('success'));

    }

    //如果是get请求,则返回上传页面
    if ($method == 'GET')
    {
        return Admin::content(function (Content $content) {

            $content->header('balabala');
            $content->description('导入数据');
            $content->body(view('Admin.GlobalUpload'));
        });
    }
}

}
3.上传按钮的定义
Extensions\GlobalUploadButton.php

<?php
namespace App\Admin\Extensions;

use Encore\Admin\Grid\Tools\AbstractTool;
use Illuminate\Support\Facades\URL;

/**

  • 全局上传数据按钮
    */

class GlobalUploadButton extends AbstractTool
{

public function __construct()
{
    $this->url = URL::current().'/upload';
}


public function render()
{
    $options = [
        $this->url   => '导入数据',
    ];

    return view('Admin.GlobalUploadButton', compact('options'));
}

}
4.上传按钮的html
resources\views\GlobalUploadButton.blade.php @foreach($options as $option => $label) {{$label}} @endforeach