如何在Laravel上传的Excel中使用时间格式Excel单元格? [英] How to use time format excel cell in laravel uploaded excel?

查看:101
本文介绍了如何在Laravel上传的Excel中使用时间格式Excel单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel软件包maatwebsite/excel 3.1版上传Excel文件.

i am using laravel package maatwebsite/excel version 3.1 for excel file upload.

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

我上传了一个文件,其中单元格的格式是时间.

i have uploaded a file where the format of cell is time.

这里的超时和超时单元格具有时间格式.当我上传此文件时,结果输出为:

Here time in and time out cell has time format. when i upload this file the resulting output is:

结果单元格中的输出超时和超时数据将转换为字符串.如何防止这种转换或如何将其从文本更改为时间?

in resulting output time in and time out cells data is converted into string. how to prevent this from converting or how to change this from text to time?

推荐答案

您需要手动将其转换或使用映射器.

You need to manually convert them or use a mapper.

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

Can use the built in function to convert one of the date as so in the following example:

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

我也可以使用Carbon

You can also use Carbon I think

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

如果您使用映射器,则可以事先定义它,不用担心.

If you use a mapper you can define that beforehand and don't worry about it.

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'],
    ];
}
}

这篇关于如何在Laravel上传的Excel中使用时间格式Excel单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆