【问题标题】:How to use time format excel cell in laravel uploaded excel?如何在laravel上传的excel中使用时间格式的excel单元格?
【发布时间】:2019-11-26 10:20:37
【问题描述】:

我正在使用 laravel 包 maatwebsite/excel 3.1 版进行 excel 文件上传。

   $collection = Excel::toCollection(new UsersImport, storage_path('app/' . $newFile));

我上传了一个单元格格式为时间的文件。

这里的时间进入和超时单元格有时间格式。 当我上传这个文件时,结果输出是:

in 结果输出时间输入和超时单元格数据被转换为字符串。 如何防止它转换或如何将它从文本更改为时间?

【问题讨论】:

    标签: php excel laravel maatwebsite-excel


    【解决方案1】:

    您需要手动转换它们或使用映射器。

    可以使用内置函数来转换日期之一,如下例所示:

    \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($collection->first()[4])
    

    我认为你也可以使用 Carbon

    Carbon::parse($collection->first()[4])
    

    如果您使用映射器,您可以预先定义它而不必担心。

    https://docs.laravel-excel.com/3.1/imports/mapped-cells.html

    namespace App\Imports;
    
    use Maatwebsite\Excel\Concerns\ToModel;
    use Maatwebsite\Excel\Concerns\WithMappedCells;
    use PhpOffice\PhpSpreadsheet\Shared\Date;
    
    new Import implements WithMappedCells, ToModel 
    {
    public function mapping(): array
    {
        return [
            'timecellA'  => 'A5',
            'timecellB' => 'A6',
            'timecellC' => 'A7',
            'timecellD' => 'A8',
    
        ];
    }
    
    public function model(array $row)
    {
        return [
             'timecellA'  => Date::excelToDateTimeObject($row['timecellA'],
             'timecellB'  => Date::excelToDateTimeObject($row['timecellB'],
             'timecellC'  => Date::excelToDateTimeObject($row['timecellC'],
             'timecellD'  => Date::excelToDateTimeObject($row['timecellD'],
        ];
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-07
      • 2017-04-18
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多