导入Excel数据时出现transformDateTime错误

问题描述 投票:0回答:2

我正在尝试使用以下代码导入 Excel:

<?php

namespace App\Imports;
use Carbon\Carbon;
use App\Vch;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::default('none');

class VchImport implements ToModel, WithHeadingRow, WithBatchInserts, WithChunkReading
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */

    private function transformDateTime(string $value, string $format = 'd-m-Y')
    {
        try {
                return Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value))->format($format);
            } 
            catch (\ErrorException $e) 
            {
                return Carbon::createFromFormat($format, $value);
            }
    }
    
    public function model(array $row)
    {
        return new Vch([
            'no_vch' => $row[1],
            'nilai_vch' => $row[2],
            'batch' => $row[3],
            'expired' => $this->transformDateTime($row[4]),
            'nama_penerima' => $row[5],
            'alamat_penerima' => $row[6],
            'status' => $row[7],
        ]);
    }
}

当我尝试导入Excel时,出现这样的错误:

App\Imports\VchImport::transformDateTime(): Argument #1 ($value) must be of type string, null given, called in C:\Apache24\htdocs\gmpro-dev\app\Imports\VchImport.php on line 41

我试过通过代码、excel更改日期格式,电脑还是不行。

我正在用

dd($row);
调试并得到这个结果:第 (4) 行它应该是日期:

array:10 [▼
  0 => null
  1 => "CP/WS-GADD/III/23-MR001799"
  2 => 100000
  3 => "0269"
  4 => 45366
  5 => null
  6 => null
  7 => "Beredar"
  8 => null
  9 => null
]
laravel
2个回答
1
投票

model
方法在 excel 文件的每一行上运行。所以当你得到 dd of row 结果属于 excel 的第一行,也许问题是由另一行引起的。

此错误的明显原因是在 excel 的一行中,第 4 行是空的。所以你需要处理这个异常:

 public function model(array $row)
    {
        return new Vch([
            'no_vch' => $row[1],
            'nilai_vch' => $row[2],
            'batch' => $row[3],
            'expired' =>$row[4]? $this->transformDateTime($row[4]):null,
            'nama_penerima' => $row[5],
            'alamat_penerima' => $row[6],
            'status' => $row[7],
        ]);
    }

或另一种方式:

 private function transformDateTime(string $value=null, string $format = 'd-m-Y')
    {
        if (!$value)
         return null ;

        try {
                return Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value))->format($format);
            } 
            catch (\ErrorException $e) 
            {
                return Carbon::createFromFormat($format, $value);
            }
    }

0
投票

您应该通过

WithValidation
关注验证行来处理此错误。您可以定义规则,例如:

  public function rules(): array
    {
        return [
            '4' => 'required',
        ];
    }

通过定义这个规则 $row[4] 不能为空

有了

SkipsOnFailure
问题,您可以控制
onFailure
函数发生验证失败时发生的事情


public function onFailure(Failure ...$failures)
    {
        // Handle the failures how you'd like.
    }

或者只使用

SkipsFailures
特性。

© www.soinside.com 2019 - 2024. All rights reserved.