I use the very useful https://github.com/Maatwebsite/Laravel-Excel package.
As I liked the idea of leaving my controllers free of excel import code, I upload the downloaded file using ExcelFile injections, see here: http://www.maatwebsite.nl/laravel-excel/docs/import#injection
This is my code for 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, my problem is that I do not understand where I use methods such as: ->selectSheets('mysheet')when using this approach.
My current job does the following in my controller after using ExcelFile to capture the file.
ExcelController.php
public function import(StudentImportFormRequest $request, StudentImport $import)
{
$results = $import->get();
foreach($results as $sheet) {
$sheetTitle = $sheet->getTitle();
if($sheetTitle === 'students') {
foreach($sheet as $row) {
}
}
}
}
, ExcelFile , selectSheets() - loadFile() - , , , , , , ?
, , , . , float (.. !), dd() Value Binder, :
CellCollection {
"name" => "John Smith"
"refno" => "s123"
"nid" => 1234567890.0
"birth_date" => Carbon {
+"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
]
}
ValueBinder, : http://www.maatwebsite.nl/laravel-excel/docs/import#formatting, . , , , :
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;
}
return parent::bindValue($cell, $value);
}
}
! .