Maatwebsite/Laravel-Excel Import converts special Characters into number 0

Viewed 3576

I have a CSV file with some rows that has text/words that contain a special character like ñ,

What happened is that when I tried to import the CSV file, the words with characters above or alike will be converted to 0.

I am using the ToModel

use Maatwebsite\Excel\Concerns\ToModel;

To reproduce this, just try to put a special character in one of the cells of your CSV file, then import it like:

Excel::import(new CsvImport, request()->file('file'));

Output : The resulting inserted data that has a special character will be stored as 0

Note: Adding data directly from the Form does not convert the special characters to 0, only when uploading CSV through import

2 Answers

On your config set the 'use_bom' to 'true'

    'csv'                    => [
        'delimiter'              => ',',
        'enclosure'              => '"',
        'line_ending'            => PHP_EOL,
        'use_bom'                => true,
        'include_separator_line' => false,
        'excel_compatibility'    => false,
    ],

This work for me:

$data = Excel::load('file.csv', false, 'ISO-8859-1');

Or in the file configuration of excel config/excel.php

      'csv'                    => [
        'delimiter'              => ';',
        'input_encoding'         => 'ISO-8859-1',
        'enclosure'              => '"',
        'line_ending'            => PHP_EOL,
        'use_bom'                => false,
        'include_separator_line' => false,
        'excel_compatibility'    => true,
    ],
Related