2

我正在尝试使用自定义格式值(http://www.maatwebsite.nl/laravel-excel/docs/import#formatting),但它不起作用。甚至 bindValue 函数对自定义类也不起作用。

Excel文件

在此处输入图像描述

Excel 活页夹类

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

文件读取逻辑

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

实际输出

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

预期产出

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

我尝试使用 setColumnFormat 但没有运气。

4

1 回答 1

0

将该代码用于询问问题 文件阅读逻辑

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();

         foreach($excel_data as $data){
            $excel_value['referenceno'] = (int)$data['referenceno'];
            $excel_datas[] = $excel_value['referenceno'];
         }
         dd($excel_datas);
     }
}
于 2017-05-24T19:03:22.797 回答