Laravel-excel: CSV delimiter

Created on 15 Oct 2014  ·  26Comments  ·  Source: Maatwebsite/Laravel-Excel

When creating custom class:

class CSVExcel  extends Excel {
    protected $delimiter  = ';';
}

And using:

 CSVExcel::load('file.csv')->export('xls');

delimiter setting is not respected

Most helpful comment

@yailperalta-santex
To use tab delimiter in csv you must set it like this "\t"
it must be in double quotes not single quotes
so configuration in config/excel.php will be like this
'delimiter' => "\t",

All 26 comments

You're mixing two things up: File injections and Facades.

You are extending the facade class, which doesn't have the $delimiter property.

What you want to do is something like:

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();
    }
}

If the docs were confusing about this, please let me know or make a pull-request for the docs :)

Docs about File Injections: http://www.maatwebsite.nl/laravel-excel/docs/import#injection

How to set Delimiter for CSV on the fly. Actually I am using "Excel::load" function in my Controller that extends BaseController. I have got success to load .XLS, .XLSX and CSV with defsult setting as mentioned in csv.php config file. But the problem is that I have to take User INput for delimiter like input can be Pipesign, Tab etc. SO how to do that? Please Help.

You can use methods like setDelimiter() to override the config settings. See: https://github.com/Maatwebsite/Laravel-Excel/blob/master/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php#L271

ok. I have used :
$results = Excel::load('uploads/' . $filename, function($reader) {

                })->setDelimiter("|")->get();

but It is reading all values in single column as It hasn't take "Pipe Delimiter". Please Help.

I'm having the same problem. Using Laravel 4. Here's the code from my controller:

public function uploadData()
{
        $file = Input::file('data')->getRealPath();

        \Excel::load($file, function($reader) {
                echo "<pre>";
                $reader->setDelimiter('|');
                print_r($reader->get());
        });
}

The output is not being split on the pipe separator, instead it's being treated as one long string. Here's a sample:

    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||
                        )

                )
)

Am I doing something wrong here? Why aren't the rows being parsed on the pipe character?

If I change the delimiter setting in config/csv.php, the file is parsed correctly. Why doesn't it work using the setDelimiter() method?

You have to change the delimiter before the file is loaded:

Excel::setDelimiter('|')->load(...);

Thank you for the help, but this is now giving an error:

iconv() expects parameter 1 to be string, object given

The error is in Shared/String.php. Is it somehow related to this?

https://github.com/Maatwebsite/Laravel-Excel/issues/382

What if you try to set it on the fly with:

Config::set('excel::csv.delimiter, '|');

It works (Laravel 5.1):

Config::set('excel.csv.delimiter', '|');

ok this way works, but why this way don't work?

Excel::load($file->getPathName(), function($reader){
    $reader->setDelimiter(';')
    dd($reader->get());
});

Because the file is already loaded, so it can't be changed anymore currently.

fine... and if i need to import 2 csv on the same request, and one use ';' as delimiter and another uses ',' i need to use Config::set 2 times? there's no other way?

Yeah, two Config::set's should work.

Version 3 will have support to change delimiter after load. But we aren't anywhere close to a release yet.

very thanks for quick reply

I am using laravel 4.2, so the default delimiter does not separate the columns on the right way if one of the column is empty

In Laravel 4, you can set the delimiter on the fly with this:
Config::set('excel::csv.delimiter', '|');

Ref: https://laravel.com/docs/4.2/packages#package-configuration

why doing override , every thing exist in 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
),

set delimiter Above Excel::load like this . Its work for me on laravel 5.3

       Excel::setDelimiter('|');

Excel::load($filename, function ($reader) {

            foreach ($reader->toArray() as $row) {

                 $data[] = $row;
            }
              print_r($data);
        });

@medamineDev config/Excel.php is for the general usage. You have to override when the system have to process files that have a different separator/delimiter.

Hi, how to set the tabulator delimiter?

Try /t

@jediscript Thanks but /t doesn't work, says that the delimiter must be 1 character.

@yailperalta-santex what problem are you trying to solve with this library? Maybe you can use http://php.net/manual/en/function.fgetcsv.php instead.

@yailperalta-santex
To use tab delimiter in csv you must set it like this "\t"
it must be in double quotes not single quotes
so configuration in config/excel.php will be like this
'delimiter' => "\t",

$data = Excel::setDelimiter("t")->load(storage_path().'/uploads/imports/data.csv')->get();

Was this page helpful?
0 / 5 - 0 ratings