Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel excel get headers and maatwebsite

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.

like image 268
Marobluns1956 Avatar asked Nov 25 '16 07:11

Marobluns1956


4 Answers

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
)
like image 146
Matija Avatar answered Oct 02 '22 00:10

Matija


This code worked for me:

$results = Excel::selectSheetsByIndex(0)->load($file)->get();
echo "<pre>";
var_dump($results->getHeading());
echo "</pre>";
like image 24
CrsCaballero Avatar answered Oct 02 '22 00:10

CrsCaballero


Try this:

/**
 * @param \SplFileInfo $file
 *
 * @return  Array
 */
public function run(SplFileInfo $file)
{
    $excelFile = Excel::load($file);

    $excelData = $excelFile->all();

    return (($excelData->first())->keys())->toArray();
}
like image 27
Mahmoud Zalt Avatar answered Oct 01 '22 23:10

Mahmoud Zalt


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.

like image 36
Saransh Kumar Avatar answered Oct 01 '22 23:10

Saransh Kumar