简体   繁体   English

如何使用 Laravel Excel 将 Excel 导入数据库

[英]How to Import Excel to Database Using Laravel Excel

I need to import attendance reports to my system using Laravel Excel Library, But I have some problem with the wrong data validation.我需要使用 Laravel Excel 库将考勤报告导入我的系统,但是我对错误的数据验证有一些问题。 For example, I have the following excel data that I want to import into my database.例如,我有以下 excel 数据要导入到我的数据库中。 在此处输入图像描述

As you can see John has put his finger three times into the machine, But I want to Insert only the first and last record into my database using Laravel excel library.如您所见,John 已将手指插入机器三次,但我只想使用 Laravel excel 库将第一条和最后一条记录插入我的数据库。 And also the struct of my database is like the following picture.我的数据库的结构也如下图所示。 在此处输入图像描述

As you can see In my DB structure I have time_in and time_out in the same row, but in the excel file time _in is in one row, and time_out is in another row, So how I can insert this excel file correctly in my Database.如您所见,在我的数据库结构中,time_in 和 time_out 在同一行中,但在 excel 文件中,时间 _in 在一行中,time_out 在另一行中,所以我如何才能在我的数据库中正确插入这个 excel 文件。

You need to parse the data before sending it to the Database (DB), not just directly feed it to your DB.您需要在将数据发送到数据库 (DB) 之前对其进行解析,而不仅仅是直接将其提供给您的数据库。 A simple explode with ' ' as the separator can split the Date/Time column then use DateTime to format the date and time, meanwhile keep tracking which of the data you need as Time In and Time Out使用' '作为分隔符的简单explode可以拆分Date/Time列,然后使用DateTime格式化日期和时间,同时跟踪您需要哪些数据作为Time InTime Out

Assuming: $date = explode(' ',$dateTime);假设: $date = explode(' ',$dateTime);

$date[0] = the date, 2/1/2021 $date[1] = the time, 7:31 $date[0] = the date, 2/1/2021 $date[1] = the time, 7:31

Assuming the date is j/n/Y , attendance_date can be filled as $attDate = date_format(DateTime::createFromFormat('j/n/Y', trim($date[0]),'Ym-d');假设日期为j/n/Y ,则attendance_date日期可以填写为$attDate = date_format(DateTime::createFromFormat('j/n/Y', trim($date[0]),'Ym-d');

may be you have to parse data before store it into database or your can change the Excel file contents可能您必须在将数据存储到数据库之前对其进行解析,或者您可以更改 Excel 文件内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM