Nabil Farhan
Nabil Farhan

Reputation: 1536

Laravel excel can't handle japanese heading properly

I am using laravel excel to upload a excel file. The file contains heading at the very top. However, when the heading is in Japanese it is not being processed properly.

For example, if my file is like

+---------+--------+-------+
| bango   | name   | level |
+---------+--------+-------+
| nihongo | 日本語 | 8     |
+---------+--------+-------+
| test01  | test01 | 12    |
+---------+--------+-------+

It gives the following output,

enter image description here

This is the correct output. However, when I change the heading to include Japanese then it doesn't work correctly. My file is like,

+---------+--------+--------+
| 番号    | 名前   | ラベル |
+---------+--------+--------+
| nihongo | 日本語 | 8      |
+---------+--------+--------+
| test01  | test01 | 12     |
+---------+--------+--------+

The output becomes

enter image description here

I tested to mix this and put some headers in English and some in Japanese. My file is like,

+---------+--------+--------+
| 番号    | name   | ラベル |
+---------+--------+--------+
| nihongo | 日本語 | 8      |
+---------+--------+--------+
| test01  | test01 | 12     |
+---------+--------+--------+

And my result becomes,

enter image description here

Although, the result gives name values correctly but the serial is incorrect. name values should be before level but it is not.

My controller function is like,

public function post($id)
    {
        $array  = (new DeliveryImport)->toArray(request('file'));
        dd($array);
    }

And my DeliveryImport.php is like,

class DeliveryImport implements ToModel, WithHeadingRow
{
    use Importable;
    public function model(array $row)
    {
    }
}

Please, note that, the code doesn't work only when there are Japanese on heading. If there is Japanese on other places then it works without a problem.

Upvotes: 0

Views: 1328

Answers (2)

Adam Ross
Adam Ross

Reputation: 11

Or you can also put these above, outside your function

use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default('none');

Upvotes: 1

Nabil Farhan
Nabil Farhan

Reputation: 1536

I have solved the problem. The solution is to change in config/excel.php.

In that file find imports' => 'heading_row'.

Then change the formatter from slug to none.

It worked flawlessly after that.

Upvotes: 2

Related Questions