์ฌ์ฉ์ ์ ์ ํด๋์ค๋ฅผ ์์ฑํ ๋:
class CSVExcel extends Excel {
protected $delimiter = ';';
}
๊ทธ๋ฆฌ๊ณ ๋ค์์ ์ฌ์ฉํฉ๋๋ค.
CSVExcel::load('file.csv')->export('xls');
๊ตฌ๋ถ ๊ธฐํธ ์ค์ ์ด ์กด์ค๋์ง ์์ต๋๋ค.
ํ์ผ ์ฃผ์ ๊ณผ ์ธ๊ด์ด๋ผ๋ ๋ ๊ฐ์ง๋ฅผ ํผํฉํ๊ณ ์์ต๋๋ค.
$delimiter ์์ฑ์ด ์๋ ํ์ฌ๋ ํด๋์ค๋ฅผ ํ์ฅํ๊ณ ์์ต๋๋ค.
๋น์ ์ดํ๊ณ ์ถ์ ๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
class MyOwnFile extends ExcelFile {
protected $delimiter = ';';
public function getFile()
{
return storage_path('exports') . '/file.csv';
}
}
class ExampleController {
public function __construct(MyOwnFile $file)
{
$this->file = $file;
}
public function import()
{
// if using fileHandlers:
$this->file->handleImport();
// If you want to fetch inside the controller (which I wouldn't recommend)
$this->file->get();
}
// or if you are using Laravel 5.0, you could use the method injection:
public function import(MyOwnFile $file)
{
$file->handleImport();
}
}
๋ฌธ์๊ฐ ์ด๊ฒ์ ๋ํด ํผ๋์ค๋ฝ๋ค๋ฉด ์ ์๊ฒ ์๋ ค์ฃผ๊ฑฐ๋ ๋ฌธ์์ ๋ํ pull-request๋ฅผ ๋ง๋์ญ์์ค :)
ํ์ผ ์ฃผ์ ์ ๋ํ ๋ฌธ์: http://www.maatwebsite.nl/laravel-excel/docs/import#injection
CSV์ ๋ํ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ฆ์์์ ์ค์ ํ๋ ๋ฐฉ๋ฒ. ์ค์ ๋ก BaseController๋ฅผ ํ์ฅํ๋ ์ปจํธ๋กค๋ฌ์์ "Excel::load" ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. csv.php ๊ตฌ์ฑ ํ์ผ์ ์ธ๊ธ๋ ๋๋ก defsult ์ค์ ์ผ๋ก .XLS, .XLSX ๋ฐ CSV๋ฅผ ๋ก๋ํ๋ ๋ฐ ์ฑ๊ณตํ์ต๋๋ค. ๊ทธ๋ฌ๋ ๋ฌธ์ ๋ ์ ๋ ฅ์ด Pipesign, Tab ๋ฑ์ด ๋ ์ ์๋ ๊ฒ๊ณผ ๊ฐ์ ๊ตฌ๋ถ ๊ธฐํธ์ ๋ํด User INput์ ๊ฐ์ ธ์์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค. ๊ทธ๋์ ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น? ๋์์ฃผ์ธ์.
setDelimiter()
์ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ์ค์ ์ ์ฌ์ ์ํ ์ ์์ต๋๋ค. ์ฐธ์กฐ: https://github.com/Maatwebsite/Laravel-Excel/blob/master/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php#L271
์ข์์. ๋๋ ์ฌ์ฉํ๋ค :
$results = Excel::load('์
๋ก๋/' . $filename, function($reader) {
})->setDelimiter("|")->get();
๊ทธ๋ฌ๋ "ํ์ดํ ๊ตฌ๋ถ ๊ธฐํธ"๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ๋จ์ผ ์ด์ ๋ชจ๋ ๊ฐ์ ์ฝ๊ณ ์์ต๋๋ค. ๋์์ฃผ์ธ์.
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. Laravel 4 ์ฌ์ฉ. ๋ด ์ปจํธ๋กค๋ฌ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public function uploadData()
{
$file = Input::file('data')->getRealPath();
\Excel::load($file, function($reader) {
echo "<pre>";
$reader->setDelimiter('|');
print_r($reader->get());
});
}
์ถ๋ ฅ์ ํ์ดํ ๊ตฌ๋ถ ๊ธฐํธ์์ ๋ถํ ๋์ง ์๊ณ ๋์ ํ๋์ ๊ธด ๋ฌธ์์ด๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค. ๋ค์์ ์ํ์ ๋๋ค.
Maatwebsite\Excel\Collections\RowCollection Object
(
[title:protected] => Worksheet
[items:protected] => Array
(
[0] => Maatwebsite\Excel\Collections\CellCollection Object
(
[title:protected] =>
[items:protected] => Array
(
[upc_numberitem_numberqty_availunit_pricedescriptioncolorqty_on_ponext_avail_dt] => 040176424354|53607-3037|0|14.50|PACK-N-GO DUFFELS 20?"|BIRDS ON A WIRE/LEAF GREEN||
)
)
[1] => Maatwebsite\Excel\Collections\CellCollection Object
(
[title:protected] =>
[items:protected] => Array
(
[upc_numberitem_numberqty_availunit_pricedescriptioncolorqty_on_ponext_avail_dt] => 040176414850|53607-3054|0|14.50|PACK-N-GO DUFFELS 20?"|BLACK/BLACK/BLACK||
)
)
)
๋ด๊ฐ ์ฌ๊ธฐ์ ๋ญ๊ฐ ์๋ชปํ๊ณ ์์ต๋๊น? ํ์ดํ ๋ฌธ์์์ ํ์ด ๊ตฌ๋ฌธ ๋ถ์๋์ง ์๋ ์ด์ ๋ ๋ฌด์์ ๋๊น?
config/csv.php
์์ delimiter
์ค์ ์ ๋ณ๊ฒฝํ๋ฉด ํ์ผ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๊ตฌ๋ฌธ ๋ถ์๋ฉ๋๋ค. setDelimiter()
๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ์๋ํ์ง ์๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
ํ์ผ์ด ๋ก๋๋๊ธฐ ์ ์ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ๋ณ๊ฒฝํด์ผ ํฉ๋๋ค.
Excel::setDelimiter('|')->load(...);
๋์์ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ํ์ง๋ง ์ด์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
iconv() expects parameter 1 to be string, object given
์ค๋ฅ๋ Shared/String.php์ ์์ต๋๋ค. ์ ์ง ์ด๊ฒ๊ณผ ๊ด๋ จ์ด ์๋ ๊ฑธ๊น์?
https://github.com/Maatwebsite/Laravel-Excel/issues/382
๋ค์์ ์ฌ์ฉํ์ฌ ์ฆ์์์ ์ค์ ํ๋ ค๊ณ ํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
Config::set('excel::csv.delimiter, '|');
์๋ํฉ๋๋ค(Laravel 5.1).
Config::set('excel.csv.delimiter', '|');
์ด ๋ฐฉ๋ฒ์ ์๋ํ์ง๋ง ์ ์ด ๋ฐฉ๋ฒ์ด ์๋ํ์ง ์์ต๋๊น?
Excel::load($file->getPathName(), function($reader){
$reader->setDelimiter(';')
dd($reader->get());
});
ํ์ผ์ด ์ด๋ฏธ ๋ก๋๋์๊ธฐ ๋๋ฌธ์ ํ์ฌ ๋ ์ด์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
์ข์์... ๋์ผํ ์์ฒญ์ ๋ํด 2๊ฐ์ csv๋ฅผ ๊ฐ์ ธ์์ผ ํ๊ณ ํ๋๋ ';'๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ ๊ตฌ๋ถ ๊ธฐํธ๋ก ๋ค๋ฅธ ํ๋๋ ','๋ฅผ ์ฌ์ฉํฉ๋๋ค. Config::set์ 2๋ฒ ์ฌ์ฉํด์ผ ํฉ๋๊น? ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์๋์?
์, ๋ ๊ฐ์ Config::set์ด ์๋ํด์ผ ํฉ๋๋ค.
๋ฒ์ 3์ ๋ก๋ ํ ๊ตฌ๋ถ ๊ธฐํธ ๋ณ๊ฒฝ์ ์ง์ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ ์์ง ๋ฆด๋ฆฌ์ค์ ๊ฐ๊น์ง ์์ต๋๋ค.
๋น ๋ฅธ ๋ต๋ณ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค
laravel 4.2๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฏ๋ก ์ด ์ค ํ๋๊ฐ ๋น์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ๊ตฌ๋ถ ๊ธฐํธ๊ฐ ์ฌ๋ฐ๋ฅธ ๋ฐฉํฅ์ผ๋ก ์ด์ ๊ตฌ๋ถํ์ง ์์ต๋๋ค.
Laravel 4์์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฆ์ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
Config::set('excel::csv.delimiter', '|');
์ฐธ์กฐ: https://laravel.com/docs/4.2/packages#package -configuration
์ฌ์ ์๋ฅผ ์ํํ๋ ์ด์ ๋ ๋ชจ๋ ๊ฒ์ด config/Excel.php์ ์์ต๋๋ค.
'csv' => array(
/*
|--------------------------------------------------------------------------
| Delimiter
|--------------------------------------------------------------------------
|
| The default delimiter which will be used to read out a CSV file
|
*/
'delimiter' => ';',
/*
|--------------------------------------------------------------------------
| Enclosure
|--------------------------------------------------------------------------
*/
'enclosure' => '',
/*
|--------------------------------------------------------------------------
| Line endings
|--------------------------------------------------------------------------
*/
'line_ending' => "\r\n",
/*
|--------------------------------------------------------------------------
| setUseBom
|--------------------------------------------------------------------------
*/
'use_bom' => false
),
์์ ๊ฐ์ด Excel::load ์์ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ค์ ํฉ๋๋ค. laravel 5.3์์ ์ ๋ฅผ ์ํ ์์
Excel::setDelimiter('|');
Excel::load($filename, ํจ์($reader) {
foreach ($reader->toArray() as $row) {
$data[] = $row;
}
print_r($data);
});
@medamineDev config/Excel.php๋ ์ผ๋ฐ์ ์ธ ์ฉ๋์ ๋๋ค. ์์คํ ์ด ๋ค๋ฅธ ๊ตฌ๋ถ ๊ธฐํธ/๊ตฌ๋ถ์๋ฅผ ๊ฐ์ง ํ์ผ์ ์ฒ๋ฆฌํด์ผ ํ๋ ๊ฒฝ์ฐ ์ฌ์ ์ํด์ผ ํฉ๋๋ค.
์๋ ํ์ธ์, ํ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ?
/t
์๋
@jediscript ๊ฐ์ฌํ์ง๋ง /t๊ฐ ์๋ํ์ง ์์ต๋๋ค. ๊ตฌ๋ถ ๊ธฐํธ๋ 1์์ฌ์ผ ํ๋ค๊ณ ๋งํฉ๋๋ค.
@yailperalta-santex ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ด๋ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๊ณ ํฉ๋๊น? ๋์ http://php.net/manual/en/function.fgetcsv.php ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
@yailperalta-santex
csv์์ ํญ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํด์ผ ํฉ๋๋ค. "\t"
์์๋ฐ์ดํ๊ฐ ์๋๋ผ ํฐ๋ฐ์ดํ์ฌ์ผ ํฉ๋๋ค.
๋ฐ๋ผ์ config/excel.php
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
'delimiter' => "\t",
$data = ์์ ::setDelimiter("t")->load(storage_path().'/uploads/imports/data.csv')->get();
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@yailperalta-santex
csv์์ ํญ ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํด์ผ ํฉ๋๋ค.
"\t"
์์๋ฐ์ดํ๊ฐ ์๋๋ผ ํฐ๋ฐ์ดํ์ฌ์ผ ํฉ๋๋ค.
๋ฐ๋ผ์
config/excel.php
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.'delimiter' => "\t",