php - Selecting a sheet using Laravel-Excel -
i'm using useful https://github.com/maatwebsite/laravel-excel package.
as liked idea of keeping controllers clear of excel import code, i'm loading uploaded file using excelfile injections, see here: http://www.maatwebsite.nl/laravel-excel/docs/import#injection
this code excelfile injection:
studentimport.php
namespace app\excel; class studentimport extends \maatwebsite\excel\files\excelfile { public function getfile() { return \input::file('student_file'); } public function getfilters() { return []; } }
however, problem don't understand run methods like: ->selectsheets('mysheet')
when using approach.
my current work around doing following in controller after using excelfile injection grab file.
excelcontroller.php
public function import(studentimportformrequest $request, studentimport $import) { $results = $import->get(); foreach($results $sheet) { $sheettitle = $sheet->gettitle(); if($sheettitle === 'students') { foreach($sheet $row) { // } } } }
i believe may need extend excelfile class , add new method wrapper around selectsheets() method or add in loadfile() method somehow - seems place filters , settings set guess might add in selection of specific sheet?
also, know how set columns strings being read numbers. have text being output floats (i.e. have following decimal point!), when dd() first , row without using value binder following:
cellcollection {#862 ▼ #title: null #items: array:8 [▼ "name" => "john smith" "refno" => "s123" "nid" => 1234567890.0 "birth_date" => carbon {#861 ▼ +"date": "1971-01-05 00:00:00.000000" +"timezone_type": 3 +"timezone": "utc" } "is_active" => 1.0 "course_title" => "computer science" "institution_id" => 1.0 "course_id" => 1.0 ] }
i've tried implementing valuebinder mentioned in docs: http://www.maatwebsite.nl/laravel-excel/docs/import#formatting have had no success yet. have 1 column needs read date, rest should read text , following code doesn't work:
namespace app\excel; use phpexcel_cell; use phpexcel_cell_datatype; use phpexcel_cell_ivaluebinder; use phpexcel_cell_defaultvaluebinder; class studentvaluebinder extends phpexcel_cell_defaultvaluebinder implements phpexcel_cell_ivaluebinder { public function bindvalue(phpexcel_cell $cell, $value = null) { if ($cell->getcolumn() !== 'd') { $cell->setvalueexplicit($value, phpexcel_cell_datatype::type_string); return true; } // else return default behavior return parent::bindvalue($cell, $value); } }
any advice appreciated! thank you.
the getfile method supposed return file name:
namespace app\excel class myexcelclass { public function getfile() { return storage_path('excel') . '/file.xls'; } }
the storage path points storage folder , whatever sub directory pass storage_path.
so studentimport->getfile(), @ least in docs, returns '/path/to/myproject/storage/excel/file.xls;.
while can separate , extend control further, let's start base case.
in controller:
use maatwebsite\excel\facades\excel; use app\excel\myexcelclass; class excelcontroller extends controller { public function importandmanipulate() { excel::load(myexcelclass::getfile(), function ($reader){ $reader->first(); // first sheet foreach($reader $sheet) // loop through sheets { $sheettitle = $sheet->gettitle(); } }); } }
you can try $reader->selectsheet('nameofsheet'). if doesn't work trying calling selectsheet on facade either after loading sheet or within callback.
i admit documentation can hard parse. why don't start storing excel file somewhere in storage path , hardcoding path load function.
Comments
Post a Comment