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

์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DB๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ค„์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ null ๊ฐ’์„ ์‚ฝ์ž…ํ•˜์‹ญ์‹œ์˜ค. ๋คํ•‘ ํ–‰ ์‡ผ:

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

๋˜๋Š” ๋” ๋‚˜์€(๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜). ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? $ํ–‰[1], $ํ–‰[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://github.com/Maatwebsite/Laravel-Excel/blob/2.1/src/config/excel.php#L374

์‚ฌ์šฉํ•˜์—ฌ ์ž‘๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.
$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 ๋“ฑ๊ธ‰