Laravel-excel: 导入 CSV 不含列标题

创建于 2017-03-07  ·  3评论  ·  资料来源: Maatwebsite/Laravel-Excel

我有这个 CSV(没有行标题)

    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

使用这个脚本:

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

我想使用此代码导入我的数据库,我无法正确获取每一行。 但插入空值。 倾倒排展示:

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

也许应该是(如何定义列名?):

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

或更好(转换为数组)。 如何获取值? $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"
      ]
    }

最有用的评论

使用它工作
$records = Excel::load(storage_path($filename), function($reader) { $reader->noHeading = true; }, 'ISO-8859-1')->get();

所有3条评论

您可以在配置中禁用它: https :

使用它工作
$records = Excel::load(storage_path($filename), function($reader) { $reader->noHeading = true; }, 'ISO-8859-1')->get();

我用这个解决了这个问题。

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

此页面是否有帮助?
0 / 5 - 0 等级