Laravel-excel: Cannot change column Format to string

Created on 7 Apr 2015  路  10Comments  路  Source: Maatwebsite/Laravel-Excel

Hi.

I'm importing data from a csv, i manage a code with leading zeros, when i pass the data from csv to database, the leading zeros are not catched.
I try to change the format data to take the code as a string, like this:

$excelData = Excel::load($csvFile)->setColumnFormat(array(
                'C' => '@'
            ))->get();

but, i got that the method doesn't exist:

production.ERROR: exception 'Maatwebsite\Excel\Exceptions\LaravelExcelException' with message '[ERROR] Reader method [setColumnFormat] does not exist.' in <laravel>/vendor/maatwebsite/excel/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php:1104

How can i change the format of the column?

Thanks.

All 10 comments

setColumnFormat is for exporting only.

Have a look at https://github.com/Maatwebsite/Laravel-Excel/issues/324. It describes a solution for an issue similiar to yours.

Thanks Patrick,
As you commented, thanks to @devinfd contribution, the fix is in version 1.3.2, so, i update to that version.
I just tested, but the problem persist:
In csv i have data as '000200010012000', so, the lib still treat it like a double, the leading zeros are missing

Did you also try the custom value binder? Because it's not included by default, you have to enable it yourself.

I just try, but i am missing doing:
Created a new file in Controllers folder with the code

<?php
use PHPExcel_Cell;
use PHPExcel_Cell_DataType;
use PHPExcel_Cell_IValueBinder;
use PHPExcel_Cell_DefaultValueBinder;

class MyValueBinderString 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_STRING);

            return true;
        }

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

Then, in controller method, before i import the csv file i do:

Excel::SetValueBinder($myValueBinder);
$excelData = Excel::load($csvFile)->get();
...

So, i think i need a little more advise here.

Thanks.

Try it like this: (no the camelcase setValueBinder)

$myValueBinder = new MyValueBinderString;
Excel::setValueBinder($myValueBinder)->load($csvFile)->get();

I change it, thanks for note.

The error a i get is about the PHPExcel_Cell use stament:

The use statement with non-compound name 'PHPExcel_Cell' has no effect 

This block:

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

is only needed when you have a namespace defined for the custom class. Else you can leave remove it from the file.

Ok, that works, now.

Your help and guidance are very appreciated.

Million Thanks,

I've bern asleep for the past few hours but it seems you got it worked out. Glad to hear it.

my excel has below cells but i am unable to convert to string, it will stored as null while importing. laravel
any help?

TJ GND BRKT AS 04/26/18

Was this page helpful?
0 / 5 - 0 ratings