Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import CSV whitout columns headers #1107

Closed
alejandri opened this issue Mar 7, 2017 · 3 comments
Closed

Import CSV whitout columns headers #1107

alejandri opened this issue Mar 7, 2017 · 3 comments

Comments

@alejandri
Copy link

i have this CSV(without row headers)

    001001,BENITEZ LI,2052,2059,2016-04-27 09:07:20
    001002,CASA PARRO,13937,13945,2016-04-21 09:07:20
    001004,NUEVO BANC,701,709,2016-04-23 22:07:20

Using this script:

    Excel::filter('chunk')->load(public_path().'/uploads/'.$filename)->chunk(250, function($results) {
                        foreach ($results as $row) {
                            $user = Lectura::create([
                                'partida' => $row->partida,
                                'nombre' => $row->nombre,
                                'lectura_ant' => $row->lectura_ant,
                                'lectura_act' => $row->lectura_act,
                                'fecha' => $row->fecha,                                       
                            ]);
                        }
                    });

I want import to my DB using this code, i cant get correctly each line. But insert null values. Dumping row show:

    CellCollection {#734 ▼
      #title: null
      #items: array:5 [▼
        "001001" => "001002"
        "benitez_li" => "CASA PARRO"
        2052 => 13937.0
        2059 => 13945.0
        "2016_04_27_090720" => "2016-04-21 09:07:20"
      ]
    }

Perhaps should be (how can define column names?):

    CellCollection {#734 ▼
      #title: null
      #items: array:5 [▼
        "partida" => "001002"
        "nombre" => "CASA PARRO"
        "lectura_ant"=> 13937.0
        "lectura_act"=> 13945.0
        "fecha" => "2016-04-21 09:07:20"
      ]
    }

or better (convert to array). How can get values? $row[1], $row[2]....

    CellCollection {#734 ▼
      #title: null
      #items: array:5 [▼
        "0" => "001002"
        "1" => "CASA PARRO"
        "2"=> 13937.0
        "3"=> 13945.0
        "4" => "2016-04-21 09:07:20"
      ]
    }
@patrickbrouwers
Copy link
Member

You can disable it in the config: https://github.com/Maatwebsite/Laravel-Excel/blob/2.1/src/config/excel.php#L374

@crarau
Copy link

crarau commented Dec 14, 2017

Got it working using
$records = Excel::load(storage_path($filename), function($reader) { $reader->noHeading = true; }, 'ISO-8859-1')->get();

@bjesua
Copy link

bjesua commented Nov 22, 2018

I solved the problem with this.

$rows = Excel::load(public_path().'/uploads/'.$filename, function($reader) {
$reader->toArray();
$reader->noHeading();
})->get();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants