setValueBinder not working when single sheet excel with maatwebsite/laravel-excel

Viewed 1453

I am trying to use Custom formatting values(http://www.maatwebsite.nl/laravel-excel/docs/import#formatting) but it is not working. Even bindValue function does not working of custom class.

Excel file

enter image description here

Excel Binder class

<?php
namespace App\Helpers;

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

class PoImportBinder 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);
    }
}

File Reading Logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\PoImportBinder;
use Excel;

class HomeController extends Controller {
     public function import(){
          $file_name = "/path/to/file/";
          $myValueBinder = new PoImportBinder();
          $excel_data = Excel::setValueBinder($myValueBinder)->load( $file_name, function($reader) {
          })->get()->toArray();
          dd($excel_data);
     }
}

Actual Output

array:2 [▼
  0 => array:1 [▼
    "referenceno" => 33251.0
  ]
  1 => array:1 [▼
    "referenceno" => 33251.0
  ]
]

Expected Output

array:2 [▼
  0 => array:1 [▼
    "referenceno" => "33251"
  ]
  1 => array:1 [▼
    "referenceno" => "33251"
  ]
]

I tried with setColumnFormat but no luck.

1 Answers
Related