Sheet selection using Laravel-Excel

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) {
                // do something
            }
        }
    }

}

, ExcelFile , selectSheets() - loadFile() - , , , , , , ?

, , , . , float (.. !), dd() Value Binder, :

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
  ]
}

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;
        }

        // else return default behavior
        return parent::bindValue($cell, $value);
    }
}

! .

+4
1

, getFile :

namespace App\Excel 
class MyExcelClass {
public function getFile()
    {
        return storage_path('excel') . '/file.xls';
    }
}

, , .

StudentImport- > getFile(), , '/path/to/MyProject/storage/excel/file.xls;.

, .

:

use Maatwebsite\Excel\Facades\Excel;
use App\Excel\MyExcelClass;
class ExcelController extends Controller
{
    public function importAndManipulate() {
        Excel::load(MyExcelClass::getFile(), function ($reader){
            $reader->first(); // get first sheet
            foreach($reader as $sheet) // loop through sheets
            {
               $sheetTitle = $sheet->getTitle();
            }
        });
    }
}

$reader- > selectSheet ('NameOfSheet'). , selectSheet , .

, . excel - .

http://www.maatwebsite.nl/laravel-excel/docs/import

-1

Source: https://habr.com/ru/post/1599105/


All Articles