Laravelexcel is converting the header values of an excel sheet. How to disable this?

Asked

Viewed 242 times

1

I am using the Laravelexcel library to import data from an excel to a database.

To get the header data from my spreadsheet, I do the following:

Excel::load('file.xls', function($reader) {

     $nomeDasColunas = $reader->get()->first()->keys();

});

But I have the following problem: When my header comes with accentuated names, hyphens and other characters, they are being converted.

For example. The values:

Nome | E-mail | Número do Cartão

Is being converted to

 nome | email | numero_do_cartao

The problem is I need the original names. I don’t want to create a function to "reconvert" the values to the original names, because that would be a shot in the foot.

I’ve looked everywhere for this solution, and I can’t find it. I even looked at the source code of the library itself, but I couldn’t find any method that I can define the way the headers are read.

Is there any configuration of this library where I can change this name conversion?

1 answer

0


I ended up discovering. There is really no method in the class Excel that performs the operation of disabling automatic conversion of column names, but it is possible to configure the library.

There are two ways to do this.

The first is when it comes to a specific case. I don’t want to disable the automatic conversion for import of all excels, but only for a specific operation. We can use the function config and set a value for excel.import.heading.

thus:

config(['excel.import.heading' => 'original']);

I used the option original to keep the original headers according to the excel sent.

The second were to change the Laravelexcel configuration file, to get these import settings globally.

To do this, just run the command php artisan vendor:publish and, after the file config/excel.php be created, edit the value of array containing the index heading (that’s inside import). The options available are:

true|false|slugged|slugged_with_count|ascii|numeric|hashed|trans|original

Browser other questions tagged

You are not signed in. Login or sign up in order to post.