UserSheet.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Imports\User;
  3. use App\Models\User;
  4. use Maatwebsite\Excel\Concerns\ToModel;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Concerns\ToCollection;
  7. class UserSheet implements ToCollection
  8. {
  9. /**
  10. * @param Collection $collection
  11. */
  12. public function collection(Collection $collection)
  13. {
  14. $user_info = [];
  15. foreach ($collection as $row) {
  16. if ($row[0]=="userId")
  17. {
  18. continue;
  19. }
  20. //用户名
  21. if ($row[1] == null)
  22. {
  23. $user_info['user_name'] = '';
  24. }else{
  25. $user_info['user_name'] = $row[1];
  26. }
  27. //昵称
  28. if ($row[2] == null)
  29. {
  30. $user_info['nickname'] = '';
  31. }else{
  32. $user_info['nickname'] = $row[2];
  33. }
  34. //电话
  35. if ($row[3] == null)
  36. {
  37. $user_info['phone'] = '';
  38. }else{
  39. $user_info['phone'] = $row[3];
  40. }
  41. if ($row[4] == null)
  42. {
  43. $user_info['created_at'] =self::excelTime(0);
  44. }else{
  45. $user_info['created_at'] = self::excelTime($row[4]);
  46. }
  47. if ($row[5] == null)
  48. {
  49. $user_info['balance'] = '';
  50. }else{
  51. $user_info['balance'] = $row[5];
  52. }
  53. User::insert($user_info);
  54. }
  55. }
  56. public static function excelTime($date, $time = false) {
  57. $date=$date>25568?$date+1:25569;
  58. $ofs=(70 * 365 + 17+2) * 86400;
  59. $d1 = date("Y-m-d H:i:s",($date * 86400) - $ofs);
  60. $d1 = strtotime("-8hours",strtotime($d1));
  61. $d1 = date('Y-m-d H:i:s',$d1);
  62. $date = $d1;
  63. return $date;
  64. }
  65. }