0

I using Laravel Excel for export CSV file in Laravel. How can I set the encoding for export csv file.

I have tried several ways:

  1. Change config in excel.php

    'use_bom' => false,
    
  2. Use mb_convert_encoding to convert content to before export.

    $exportData = mb_convert_encoding($exportData, "SJIS", "UTF-8");  
    $pblClassExport = new \App\Exports\PblClassExport($exportData, 'test.csv');
    

But it's not working. The encoding of csv file auto change by file content.

3
  • why you need to set ? encoding ? Commented Jan 22, 2021 at 3:39
  • @KamleshPaul Thank for your response. I'm using a third-party service and the service required upload csv file has encoding is SJIS . Commented Jan 22, 2021 at 5:27
  • I posted your solution on answer section and made it community wiki. Feel free to edit and accept own solution. Commented Nov 29, 2022 at 2:46

3 Answers 3

1

you need to configure your PblClassExport.php headers

in PblClassExport.php


/**
 * Optional headers
*/
private $headers = [
    'Content-Type' => 'text/csv',
    'Content-Encoding'=> 'SHIFT-JIS' // somthing like this ?
];

i have't done this but i think it will work

ref link

https://docs.laravel-excel.com/3.1/exports/exportables.html#exportables


Update

you can encode line by line

public function bindValue(Cell $cell, $value)
{
    $value = mb_convert_encoding($value, "SJIS");
    return parent::bindValue($cell, $value);
}

ref link https://www.gitmemory.com/issue/Maatwebsite/Laravel-Excel/1886/552849170

Sign up to request clarification or add additional context in comments.

3 Comments

thank for your answer. I tried to add it to header. But It's not working. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…
@ThanhNguyen i think you should use this github.com/SUKOHI/FluentCsv
Thank you so much! I also tried it before, but I met the same issue with this issue. stackoverflow.com/questions/60292152/…
0

If you you use Laravel Excel 3.1, you can use WithCustomCsvSettings interface. Then you add your encoding setting in the getCsvSettings method like the following.

use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
class InvoicesExport implements WithCustomCsvSettings
{
   public function getCsvSettings(): array
   {
        return [
            'output_encoding' => 'SJIS',
         ];

    }
}

Plese refer to the docs for more details.

Comments

0

I resolved it. Let's me share my solution here.

Laravel Excel not support it by default.But we can do it by simple way.

  1. Get csv content before download: \Excel::raw
  2. Convert to another encoding: mb_convert_encoding https://docs.laravel-excel.com/3.1/exports/collection.html#storing-raw-contents
  3. Download csv.
    $exportedObject= new \App\Exports\ClassExport($exportDataArray, $fileName);  
    $csvContent = \Excel::raw($exportedObject, $exportedObject->writerType);
    
    
    $csvContent = mb_convert_encoding($csvContent, 'SJIS', 'auto');  
    
    // In my case, I upload my csv to S3.
    $storageInstance = \Storage::disk('s3_import_csvs');  
    $putFileOnStorage = $storageInstance->put($fileName, $csvContent);
    

Note: Solution povided by OP on question section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.