Laravel-excel: Importar CSV sem cabeçalhos de colunas

Criado em 7 mar. 2017  ·  3Comentários  ·  Fonte: Maatwebsite/Laravel-Excel

eu tenho este CSV (sem cabeçalhos de linha)

    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

Usando este 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,                                       
                            ]);
                        }
                    });

Quero importar para meu banco de dados usando este código, não consigo obter cada linha corretamente. Mas insira valores nulos. Mostrar linha de despejo:

    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"
      ]
    }

Talvez devesse ser (como definir os nomes das colunas?):

    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"
      ]
    }

ou melhor (converter em array). Como posso obter valores? $ linha [1], $ linha [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"
      ]
    }

Comentários muito úteis

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

Todos 3 comentários

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

Resolvi o problema com isso.

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

Esta página foi útil?
0 / 5 - 0 avaliações