如何获取 Laravel [Maatwebsite/Laravel-Excel] 中的特定列

How to get specific columns in Laravel [Maatwebsite/Laravel-Excel]

我正在导入的文件有数千条记录,这导致我的网络变慢。在将其插入数据库之前,我只想阅读我需要的那些列。处理文件时,它应该首先搜索那些列而不是整个文件,然后获取那些列的行

Excel::load($path, function($reader) {

//Getting headers using this


       $headers = $reader->first()->keys()->toArray();

//This is the array of required columns

       $headings = array('registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city');


});

读取文件后插入数据

 if($data->count() > 0)
             {
              foreach($data->toArray() as $value)
              {
                $insert[] = array(
                 'registrant_name'  => $value['registrant_name'],
                 'registrant_address'   => $value['registrant_address'],
                 'registrant_phone'   => $value['registrant_phone'],
                 'registrant_zip'   => $value['registrant_zip'],
                 'registrant_email'   => $value['registrant_email'],
                 'registrant_country'   => $value['registrant_country'],
                 'registrant_state'   => $value['registrant_state'],
                 'registrant_city'   => $value['registrant_city']

                );                
               }
              }


      if(!empty($insert))
      {
       DB::table('customers')->insert($insert);
      }    

在集合中,您可以检查方法 documentation

$headers = $reader->first()->only(['registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city']);

您也可以使用 chunk,将数据插入到较小的集合中而不是大集合中。

您能否将 post 更新为 App\Import class 以便为您提供更多帮助。