david001's avatar

How to escape additional headers in xlsx file

Hi, iam using laravel excel reader https://github.com/Maatwebsite/Laravel-Excel to upload xlsx file. it is working fine.But i have one problem.Problem is what if user added a extra rows in excel file and uploads the file ,my current code gives error for this situation.How can i ignore user defined extra fields and save file in database table ,how should i change my code please help me.

currently there are two rows billing_operator and billing_operator_name,if user add extra rows in excel file say udf1 and `````udf2 ,it needs to ignore this fields and and uploads . onlybilling_operatorandbilling_operator_name```

public function postOperatorFiles(OperatorFileRequest $request)
    {
    if($request->hasFile('file'))
        {
            $fileName= rand(1000,10000). '.' . $request->file('file')->getClientOriginalExtension();
        
            $filePath = base_path() . '/crosswalk/';

            $request->file('file')->move($filePath ,$fileName );
        
            $filePath.= $fileName;

            $results = \Excel::load($filePath)->get(); 
            $date = date('Y-M-D');
           

            foreach($results as $rows)
            {
                foreach($rows as $row)
                {             
                    $this->operatorFiles->create([
                    
                        'source_file_name'=>$filePath,
                        'imported_date'=>$date,
                        'operator_code'=>$row->billing_operator,
                        'operator_name'=>$row->billing_operator_name
                       
                    ]);

                
               }

            }

      
        }
        return redirect::back()->with(['message'=>' File Uploaded SuccessFully!']);
    }
}

If file contains any extra headers, it throws exception. How to handle that expection

0 likes
4 replies
david001's avatar

Can anyone help me please,how i need to change current code and database table so file get uploads and save in database when user add extra rows

Snapey's avatar

currently there are two rows billing_operator and billing_operator_name,if user add extra rows in excel file and uploads they it gives error.

two rows? Did you mean two columns?

Are they adding more rows or more columns?

What does the import look like?

david001's avatar

yes if user add extra two headers named ufd1 and ufd2 i need escape this fields and uploads file

Please or to participate in this conversation.