Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandan's avatar
Level 13

import csv file without header lines how it work?

$path=Input::file('import_file')->getRealPath();

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

})->get();

if(!empty($data) && $data->count())
    
{
        
    foreach($data as $key=>$value)
        
    {
            
        $data=$data->toArray();
            
        for($i=0;$i<count($data);$i++)
                {
    
                        $dataImported[]=$data[$i];
            
        }        
                
        Import::insert($dataImported);
            

return \Redirect::Back(); } }

0 likes
5 replies
Sabonzy's avatar
Sabonzy
Best Answer
Level 9

change the startRow in the excel.php in the config to match the row your data starts

'import'     => [
  
        |--------------------------------------------------------------------------
        | First Row with data or heading of data
        |--------------------------------------------------------------------------
        |
        | If the heading row is not the first row, or the data doesn't start
        | on the first row, here you can change the start row.
        |
        */
        'startRow'                => 1,
]

vandan's avatar
Level 13

@SABONZY - thank you bro

but when i try to data export so output will not consider header how to fix it

Sabonzy's avatar

using the users table as an example you can format the data to include the headers.

 $users = \App\User::all()->map(function ($$user) {
                return [
                         'NAME'          => $user->name,
                         'EMAIL'          =>$user->email
                       ];
            });



Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        $sheet->fromArray($users);

    });

})->export('xls');
vandan's avatar
Level 13

@SABONZY - not working in laravel 5.5 or excel 3.1 maatwebsite packages can you suggest how to other solution

Sabonzy's avatar

@VANDAN - this only apply to excel 2.1 but with excel 3.1 you have to implement the WithHeadings and WithMapping concerns. check the docs

Please or to participate in this conversation.