网络知识 娱乐 Laravel Excel(maatwebsite/excel )导入

Laravel Excel(maatwebsite/excel )导入

一:安装

1、要求:

2、安装命令:

使用Composer安装依赖
composer require maatwebsite/excel

使用Composer安装依赖 指定版本
composer require maatwebsite/excel=~3.1

3、配置修改:

将 ServiceProvider 添加到config/app.php 中注册服务提供者到 providers 数组:

'providers' => [
    /*
     * Package Service Providers...
     */
    MaatwebsiteExcelExcelServiceProvider::class,
]

在 config/app.php 中 aliases 数组添加 Facade:

'aliases' => [
    ...
    'Excel' => MaatwebsiteExcelFacadesExcel::class,
]

如果想要进行更多的自定义配置,执行如下Artisan命令:

php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider" --tag=config

执行成功后会在 config 目录下生成一个配置文件 excel.php

到这里,安装就完成了

二:导入

1、创建一个导入类,可以使用make:import命令来执行此操作。

php artisan make:import ImportTest --model=TestImport

名称、模型自行定义,我使用的是导入 Eloquent 模型ToModel,我的一些代码参考

 $row[0],
            'remarks' => $row[1],
        ]);
        
    }

     /**
     * @author cjg
     * rules()方法期望返回一个带有 Laravel 验证规则的数组。
     */
    public function rules(): array
    {
        return [
            // Can also use callback validation rules 
            '0' => function($attribute, $value, $onFailure) {
                if ($value != '手机号') {
                    if (!$value || !preg_match("/^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])d{8}$/", $value)) {
                        $onFailure('手机号错误');
                    }
                }
            },
            '1' => function($attribute, $value, $onFailure) {
                if ($value != '备注' && !$value) {
                    $onFailure('备注不能为空');
                }
            },
        ];
    }

}

xlsx数据截图

2、导入控制器代码

    /**
     * 导入代码
     * @author cjg
     */
    public function test_import(Request $request)
    {
        $file = $request->file('file');

        if(substr(strtolower(PHP_OS), 0, 3) == 'win') {
            //获取文件临时目录 windows环境
            $realpath = $file->getRealPath();
        } else {
            //获取文件临时目录 linux环境
            $realpath = storage_path('app') . '/' . $file->store('temp');
        }

        // 获取文件名,此方法包括后缀
        // $filename = $file->getClientOriginalName();
        // 将缓存在tmp目录下的文件移到某个位置,返回的是这个文件移动过后的路径
        // $path=$file->move(文件移到哪个文件夹下的路径, 将上传的文件重新命名的文件名);
        // 获取上传的文件缓存在tmp文件夹下的绝对路径
        // $realpath=$file->getRealPath();

        $import = new ImportTest();
        $import->import($realpath);
        if ($import->failures()) {
            //每个失败都是一个实例MaatwebsiteExcelValidatorsFailure。Failure保存有关该单元格的哪一行、哪一列以及验证错误的信息。
            foreach ($import->failures() as $failure) {
                // 出问题的那一行
                $failure->row();
                //标题键(如果使用标题行)或列索引
                $failure->attribute();
                //来自Laravel验证程序的实际错误消息
                $failure->errors();
                //失败行的值。
                $failure->values();
            }

        }
    }

3、路由记得加上,用post方法

导入就这么简单,更多详情可以参考官方文档

官方文档:Introduction | Laravel ExcelSupercharged Excel exports and imports in Laravelhttps://docs.laravel-excel.com/3.1/getting-started/