第一步:

composer require maatwebsite/excel

第二步
$app->register(Maatwebsite\Excel\ExcelServiceProvider::class);
发到app.php里面

laravel 最新Maatwebsite导入excel文件

 

 

建立我自己的模型类

  1. <?php
  2.  
  3.  
  4. namespace App\Imports;
  5.  
  6. use Illuminate\Support\Collection;
  7. use Maatwebsite\Excel\Concerns\ToArray;
  8.  
  9.  
  10. class UsersImport implements ToArray
  11. {
  12. public function Array(Array $tables)
  13. {
  14. return $tables;
  15. }
  16.  
  17. }

  我就是这样写的~ 也还没研究透彻怎么用

第二步直接引用就行了(我主要是导入excel 文件使用的)

然后就是主要用导入的方法引用模型就行

导入的方法 要和你写的导入模型内容一致

导入方法有 

$array = Excel::toArray(new UsersImport, 'users.xlsx');
$collection = Excel::toCollection(new UsersImport, 'users.xlsx');

 以上两种 ,引用你创建的模型,就可以直接获得导入的接口了。我用的toArray;

  1.  
  2. public function BatchUpload(Request $request)
  3. {
  4.  
  5. $file = $request->file('excel'); //获取UploadFile实例
     
    可以使用本地路径尝试一下:$file = 文件路径。
  6.  
  7. $data = Excel::toArray(new UsersImport,文件路径);
  8. dd($data);
  9.  
  10. // 加工数据$data;
  11.  
  12. }
     

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-15
  • 2021-04-22
相关资源
相似解决方案