laravel Excel 導入


 1 <?php 
 2 namespace App\Modules\Live\Http\Controllers;
 3 
 4 use Illuminate\Http\Request;
 5 use Maatwebsite\Excel\Facades\Excel;
 6 class ImportController extends Controller
 7 {
 8     public function import(){
 9         try {
10             
11             $request = request();
12             $file = $request->file("file");
13             $originalName = $file->getClientOriginalName(); // 文件原名
14             $realPath = $file->getRealPath();
15             Excel::load($realPath, function($reader) use($file){
16                 $data = $reader->getSheet(0)->toArray();
17                 foreach($data as $key => $value){
18                     if($key !=0){
19                         $tmp['realname']=$value[0];
20                         $tmp['mobile']=$value[1];
21                         $tmp['idcard']=$value[2];
22                         $tmp['passport']=$value[3];
23                         $tmp['company_name']=$value[4];
24                         $tmp['uniacid']=4;
25                         $tmp['created_at']=time();
26                         User::create($tmp);
27                     }
28                 }
29             });
30             $this->_addLog('導入數據');
31             return $this->result(0,'導入成功',['redirect' => wgRoute('member.index')]);
32         }catch(Exception $e){
33             return $this->error($e);
34         }
35     }
36 }
37 ?>

如果需要在導入Excel數據中傳遞其他參數,可通過use()方法,以數組的形式進行傳遞

Excel::load($realPath, function($reader) use($options){}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM