2

Hi i'm using Maatwerk Excel laravel package to export data to XLSX and CSV.

In 2 instances a comma is good.

But now i need to make a CSV where the delimeter is not a comma but something different (a tab or pipe symbol).

I cannot find where to set this. I tried:

Config::set('Excel::csv.delimeter','|');

Excel::create('CSV Products', function($excel) use ($exports_arr) {
    $excel->setTitle('Products');
    $excel->setCreator('Me')->setCompany('My company');
    $excel->setDescription('Products');
    $excel->sheet('sheet1', function($sheet) use ($exports_arr) {
        $sheet->fromArray($exports_arr, null, 'A1', false, false);
    });
})->download('csv');

But if i look in the config/Excel.php file the comments suggest that this delimeter is only for reading.

Is it even possible to change the Delimeter for EXPORTING CSV files?

Thanks in advance.

Rubberduck1337106092
  • 1,294
  • 5
  • 21
  • 38

4 Answers4

3

The comment states that excel.csv.delimiter is used for reading out a csv file, but in Writers/LaravelExcelWriter.php (line 578) the CSV delimiter is taken from the config, and set as , by default:

$this->writer->setDelimiter(config('excel.csv.delimiter', ','));

Are you sure the Config::set statement works properly?

Try to use:

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

and check the value with

Config::get('excel.csv.delimeter');

UPDATE:

As mentioned in this answer, the service provider is registered before the request takes place. Updating the config key during the request won't affect the value that is read earlier by Maatwerk/Excel. A solution is given in the answer, by creating a deferred provider.

piscator
  • 8,028
  • 5
  • 23
  • 32
  • Hey, when i change the Delimeter inside the /config/Excel.php it works! But it doesn't work with `Config::set` I checked it with `Config::get` and returns the correct symbel i set before. But the Excel::create uses the default comma... – Rubberduck1337106092 Nov 09 '17 at 13:40
  • You are right, it won't work because the service provider is already registered and the config values are already read. See more about this issue [here](https://stackoverflow.com/questions/39563042/laravel-dynamic-config-settings) – piscator Nov 09 '17 at 15:52
3

I know this is a bit outdated but I was having the same problem recently.

In order to set a custom delimiter while exporting multiple CSV files, you can create a new instance of the use Maatwebsite\Excel\Excel class without using the facade.

Try this:

use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Writer; 
use Maatwebsite\Excel\QueuedWriter; 
use Maatwebsite\Excel\Reader; 

...

$reader = new Reader(app()->make('filesystem'));    
$writer = new Writer; 
$queued_writer = new QueuedWriter($writer); 

$writer->setDelimiter('|');

$excel = new Excel($writer, $queued_writer, $reader, app()->make('filesystem'));
$excel->create( ... );
2

An update on this question: If you are using Laravel Excel 3, you can set it in the config/excel.php file:

return [
   'exports' => [
        'csv' => [
            'delimiter' => '|',
        ]
   ]
]

Or if you want to set it dynamically:

\Config::set('excel.exports.csv.delimiter', '|');
Sogeking
  • 780
  • 8
  • 19
0
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithCustomQuerySize;


class MyExportClass implements FromView, WithCustomQuerySize, WithCustomCsvSettings
{
    use Exportable;

 
    public string $filePath;
    public string $disk;

    public function getCsvSettings(): array
    {
        return [
            'delimiter' => ",",
        ];
    }
....
}

From documentation: enter link description here