laravel excel获取标题和maatwebsite
我只想得到我的excel第一列将是列名。
我正在使用这个代码
Excel::load($request->file, function ($reader) use($request) { $torarray=$reader->toArray(); $line0 = $torarray[0]; $headers = array_keys($line0); $excel_header=$headers; });
有时它可以工作,但有时不起作用。 当不工作与一些文件,我写在下面,并工作
$torarray=$reader->toArray(); $line0 = $torarray[0][0]; $headers = array_keys($line0); $excel_header=$headers; });
我不知道最新的解决scheme是什么。
尝试这个:
/** * @param \SplFileInfo $file * * @return Array */ public function run(SplFileInfo $file) { $excelFile = Excel::load($file); $excelData = $excelFile->all(); return (($excelData->first())->keys())->toArray(); }
谢谢@ mahmoud-zalt
只是想分享我在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 )
这段代码适用于我:
$results = Excel::selectSheetsByIndex(0)->load($file)->get(); echo "<pre>"; var_dump($results->getHeading()); echo "</pre>";