Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

haakym's avatar

Selecting a sheet using Laravel-Excel and Formatting Columns

Hey everyone,

I'm using the very useful https://github.com/Maatwebsite/Laravel-Excel package.

As I liked the idea of keeping my controllers clear of the excel import code, I'm loading the uploaded file using ExcelFile injections, see here: http://www.maatwebsite.nl/laravel-excel/docs/import#injection

This is my code for the 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 don't understand where I run methods like: ->selectSheets('mysheet') when using this approach.

My current work around is doing the following in my controller after using the ExcelFile injection to grab the file

ExcelController.php

$results = $import->get();

foreach($results as $sheet) {
    $sheetTitle = $sheet->getTitle();

    if($sheetTitle === 'students') {
        foreach($sheet as $row) {
            // do something
        }
    }
}

I believe I may need to extend the ExcelFile class and add a new method which will be a wrapper around the selectSheets() method or add it in to the loadFile() method somehow - this seems to be the place where the filters and settings are set up so I guess this is where you might add in a selection of a specific sheet?

Also, I would like to know how to set certain columns to strings as they currently being read as numbers. Currently I have text being output as floats (i.e. they have a following decimal point!), when I dd() the first and only row without using a Value Binder I get the 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 a ValueBinder as mentioned in the docs: http://www.maatwebsite.nl/laravel-excel/docs/import#formatting but have had no success yet. I only have one column which needs to be read as a date, the rest should be read as text and the 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 would be most appreciated! Thank you.

0 likes
6 replies
davidjoan's avatar

Hi Follow this example, from the reader you can access any sheet, it depend of the number of sheet in you file.xls

$data = 'data';
Excel::load('/storage/app/file.xls', function($reader) use($data){
foreach($reader as $key => $sheet) {
    $sheetTitle = $sheet->getTitle();
   $reader->setActiveSheetIndex($key);
    if($sheetTitle === 'students') {
     $reader->getActiveSheet()->setCellValue('A8', $data);
         //or $sheet->setCellValue('A8', $data);
     }
  }
})->export('xls');
haakym's avatar

@davidjoan thanks for your comment, BUT I fail to see how your reply addresses the issue of how I can use the selectSheet method with ExcelFile Injection. I've updated my question to make it clearer what I'm trying to do.

davidjoan's avatar

Ok I understand If you implement the StudentImport only serve for get the information of some excel file uploaded in a form or in your disk. If you want to manage the information you need tu implement this class

class StudentExport extends \Maatwebsite\Excel\Files\NewExcelFile {

    public function getFilename()
    {
        return 'filename';
    }
}

it's in the documentation http://www.maatwebsite.nl/laravel-excel/docs/export#injection

and the rigth way to manage sheets is like this

class ExampleController extends Controller {

    public function exportUserList(StudentExport $export)
    {
        // work on the export
        return $export->sheet('students', function($sheet)
        {

        })->export('xls');
    }
}
shiroamada's avatar

Any one have sample how to read the sheet row by row, I can't understand the example provided by the author. Any sample tutorial.

From the documentation: Selecting columns If you want to select only a couple of columns, you can use ->select($columns) or pass an array as the first parameter of ->get($columns).

// Select $reader->select(array('firstname', 'lastname'))->get();

// Or $reader->get(array('firstname', 'lastname'));

Normally reading from excel is number and alphabet, eg. row 1,2,3, and column A,B,C

My goal is read excel and import to database.

1 like
rohmanhm's avatar

Hy, can i read excel data with select the cell? Iterate not usefull for selecting cell data.

jimschlies's avatar

Like shiroamada, I am unable to I can't understand the example provided by the author.

Please or to participate in this conversation.