I want to get only my excel first column that will be column name.
I am using this code
Excel::load($request->file, function ($reader) use($request) {
$torarray=$reader->toArray();
$line0 = $torarray[0];
$headers = array_keys($line0);
$excel_header=$headers;
});
Sometimes it works but some times not work. when not work with some files the i write below that and works
$torarray=$reader->toArray();
$line0 = $torarray[0][0];
$headers = array_keys($line0);
$excel_header=$headers;
});
I cant understand whats the correct solution.
Thank you @mahmoud-zalt
Just wanted to share how I use it in Laravel 5.5:
$reader = Excel::load(storage_path() . '/uploads/tracking-number/' . $fileName)->get();
$headerRow = $reader->first()->keys()->toArray();
JSON
:
["date","reference_no","tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post","destination_country","receiver","tel","state","city","post_code","address","weightkg","quantity","valueusd","description","parcelquantity",0]
var_export
:
array (
0 => 'date',
1 => 'reference_no',
2 => 'tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post',
3 => 'destination_country',
4 => 'receiver',
5 => 'tel',
6 => 'state',
7 => 'city',
8 => 'post_code',
9 => 'address',
10 => 'weightkg',
11 => 'quantity',
12 => 'valueusd',
13 => 'description',
14 => 'parcelquantity',
15 => 0,
)
print_r
Array
(
[0] => date
[1] => reference_no
[2] => tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post
[3] => destination_country
[4] => receiver
[5] => tel
[6] => state
[7] => city
[8] => post_code
[9] => address
[10] => weightkg
[11] => quantity
[12] => valueusd
[13] => description
[14] => parcelquantity
[15] => 0
)
This code worked for me:
$results = Excel::selectSheetsByIndex(0)->load($file)->get();
echo "<pre>";
var_dump($results->getHeading());
echo "</pre>";
Try this:
/**
* @param \SplFileInfo $file
*
* @return Array
*/
public function run(SplFileInfo $file)
{
$excelFile = Excel::load($file);
$excelData = $excelFile->all();
return (($excelData->first())->keys())->toArray();
}
For version 3 of maatwebsite excel you can get the headings by the following approach:
use Maatwebsite\Excel\HeadingRowImport;
$headings = (new HeadingRowImport)->toArray(file);
Insert your file path in place of the file keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With