5

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.

Marobluns1956
  • 51
  • 1
  • 1
  • 3

5 Answers5

11

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
)
Matija
  • 17,604
  • 2
  • 48
  • 43
  • Maybe you can help me. Look at this : https://stackoverflow.com/questions/51685886/how-can-i-add-title-and-sum-value-of-column-in-laravel-excel-version-3 – moses toh Aug 04 '18 at 13:12
8

This code worked for me:

$results = Excel::selectSheetsByIndex(0)->load($file)->get();
echo "<pre>";
var_dump($results->getHeading());
echo "</pre>";
CrsCaballero
  • 2,124
  • 1
  • 24
  • 31
2

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.

Saransh Kumar
  • 421
  • 4
  • 6
  • But if I just have the file in $request->file('file') and no file path? Do I need to store it first? – Phil Dec 29 '20 at 14:40
  • Hi @Phil, no, you can pass in the `$request->file('filename')`, e.g. `$headings = (new HeadingRowImport)->toArray($request->file('filename'));` should work – avn Oct 19 '21 at 19:12
1

Try this:

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

    $excelData = $excelFile->all();

    return (($excelData->first())->keys())->toArray();
}
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • Maybe you can help me. Look at this : https://stackoverflow.com/questions/51685886/how-can-i-add-title-and-sum-value-of-column-in-laravel-excel-version-3 – moses toh Aug 04 '18 at 13:12
0

Just wanted to share a bit more optimized approach (this is v2.1 solution):

Excel::load($file->path(), function (LaravelExcelReader $reader) {

    /** @var RowCollection $singleRow */
    $singleRow = $reader->takeRows(1)->get(); // no need to parse whole sheet for the headings
    $headings = $singleRow->getHeading();

    // Do validation. Throw exception.

    $reader->takeRows(false); // set the limit back to unlimited
})->get();
Andrius Rimkus
  • 643
  • 5
  • 10