Laravel-excel: laravel excel print headers and maatwebsite

Created on 5 Aug 2015  路  18Comments  路  Source: Maatwebsite/Laravel-Excel

Hello,
This library is very good.
I appreciate your help with the following query. I'm currently using laravel 5 maatwebsite to read excel file the following:

ID NAME PRICE CITY
1 PETER 100 MADRID
2 GEORGE 300 CALIFORNIA
3 JEANET 50 NEW YORK
4 MARK 200 BOSTON

I would like to read the headers and print as follows:

RESULT:

ID
NAME
PRICE
CITY

I appreciate your support.
Thank You.

Most helpful comment

/**
 * @param \SplFileInfo $file
 *
 * @return  Array
 */
public function getHeaders(SplFileInfo $file)
{
    return ((((Excel::load($file))->all())->first())->keys())->toArray(); // I wrap in 1 line just for fun :)
}

All 18 comments

Hey,

I don't really get your question. Can you try to explain it a bit more. Thanks!

Hello,
I desire to capture the excel file headers to validate compliance with the structure of a table. After that the file was imported to mysql excel.

You could use array_keys on the first row, it will return the keys of the array which will be id,name,price,city

Thank you for responding patrickbrouwers, I do follows:

Excel::load('public/storage/datos.xlsx',function($file){
$rs=$file->get();
$line0 = $rs[0];
$headers = array_keys($line0);
echo dd($headers);
});

I get this error ... array_keys() expects parameter 1 to be array, object given

$line0 is a Collection instance, you could try to use $line0->keys()

Excellent patrickbrouwers. Thanks for the help

@patrickbrouwers array_keys that is totally true but it creates slug of the header column what if i want that header as it is?

@SagarNaliyapara you can change excel setting import array change value of heading => original instead of true or slug in laravel directory config/excel file under

What we will do if we don't know the column name?

how will i dynamically load column names say store column names in an array

/**
 * @param \SplFileInfo $file
 *
 * @return  Array
 */
public function getHeaders(SplFileInfo $file)
{
    return ((((Excel::load($file))->all())->first())->keys())->toArray(); // I wrap in 1 line just for fun :)
}

Thanks boss

@Mahmoudz : I would like to know whether the format of the column names that are outputted by your code is in camel_case or snake_case. Because, for the column name 'Coventry NE/IA' in the excel header, it is being read as 'coverntry_neia'. I'm implementing some other logic in my code which works only if I get to know before hand, how the column name is read. I can see that the interpretation of 'Coventry NE/IA' as 'coverntry_neia' is neither snake_case nor camel_case. Anybody's help on this query of mine is appreciated.

'

@Mahmoudz, could you explain how to use your function?
i use by this:

$header=$this->getHeaders($reader);
i got error

Type error: Argument 1 passed to App\Http\Controllers\PesertaController::getHeaders() must be an instance of App\Http\Controllers\SplFileInfo, instance of Maatwebsite\Excel\Readers\LaravelExcelReader given, called in F:\Development\laragon\www\dms\app\Http\Controllers\PesertaController.php on line 29.

what do you mean splfileinfo?

This code worked for me

Excel::load($file, function($reader){
    var_dump($reader->get()[0]->getHeading());
});

@patrickbrouwers how can i get header in 3.1ver?

@patrickbrouwers how can i get header in 3.1ver?

@TheThai212 Please refer to https://laravel-excel.maatwebsite.nl/3.1/imports/heading-row.html#importing-only-the-heading-row

Check the official docs. I think this is what you are looking for
https://docs.laravel-excel.com/3.1/imports/mapped-cells.html#mapped-cells

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhghazaliakbar picture muhghazaliakbar  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

ellej16 picture ellej16  路  3Comments

octoxan picture octoxan  路  3Comments

gamevnlc picture gamevnlc  路  3Comments