gpt4 book ai didi

php - setValueBinder 在使用 maatwebsite/laravel-excel 时单张 excel 时不起作用

转载 作者:行者123 更新时间:2023-12-05 07:42:51 25 4
gpt4 key购买 nike

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

Excel文件

enter image description here

Excel Binder 类

<?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 但没有成功。

最佳答案

Use that code for Ask Question File Reading Logic

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

关于php - setValueBinder 在使用 maatwebsite/laravel-excel 时单张 excel 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44162652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com