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