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

squibby's avatar

Importing boolean columns with maatwebsite/excel

I am importing csv row data using maatwebsite/excel package. In my csv file if i put a 1 or 0 in my boolean columns they are imported with a data type of double (when i log to check). When i then convert the collection to an array they become 1.0 or 0.0 and fail the validation checks I have set up for boolean values when I try to create a new model record.

Is there any way I can force the datatype to be boolean for the imported maatwebsite/excel rows.

Thanks.

0 likes
3 replies
quickliketurtle's avatar
Level 16

@squibby From the doc's http://www.maatwebsite.nl/laravel-excel/docs/import

Custom formatting values

By default Laravel Excel uses PHPExcel's default value binder to intelligently format a cells value when reading it. You may override this behavior by passing in your own value binder to suit your specific needs. Value binders must implement PHPExcel_Cell_IValueBinder and have a bindValue method. They may also extend PHPExcel_Cell_DefaultValueBinder to return the default behavior.

use PHPExcel_Cell;
use PHPExcel_Cell_DataType;
use PHPExcel_Cell_IValueBinder;
use PHPExcel_Cell_DefaultValueBinder;

class MyValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        if (is_numeric($value))
        {
            $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);

            return true;
        }

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

$myValueBinder = new MyValueBinder;

$reader = Excel::setValueBinder($myValueBinder)->load('file.xls');
// Reader methods
Available PHPExcel_Cell_DataType's are TYPE_STRING, TYPE_FORMULA, TYPE_NUMERIC, TYPE_BOOL, TYPE_NULL, TYPE_INLINE and TYPE_ERROR

To reset the value binder back to default and/or before calling Laravel Excel after setting a custom value binder you need to call the resetValueBinder method.

Excel::resetValueBinder();
squibby's avatar

In the following block is there any way to grab the column header for the column the cell belongs to?

I would like to return boolean data types only for specific columns.

public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        if (is_numeric($value))
        {
            $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);

            return true;
        }

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

I have found reference here:

http://www.osakac.ac.jp/labs/koeda/tmp/phpexcel/Documentation/API/PHPExcel_Cell/PHPExcel_Cell.html#methodgetColumn

I can get the column with $cell->getColumn() but that will just return the column letter e.g. D

If there is someway to return the header name I could check against an array of column headers I want to specify as boolean and return that for each one that passed.

Please or to participate in this conversation.