ImportHeadingExcel.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: luxf
  5. * Date: 2022/3/29
  6. * Time: 9:53 AM
  7. */
  8. namespace App\ExcelData\Services;
  9. use Illuminate\Support\Collection;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. /**
  13. * 通用导入EXCEL文件对象
  14. * Class ImportExcel
  15. * @package App\Base\Models
  16. */
  17. class ImportHeadingExcel implements ToCollection
  18. {
  19. private $callback;
  20. private $readRows = 0;
  21. public function __construct(\Closure $callback)
  22. {
  23. $this->callback = $callback;
  24. }
  25. /**
  26. * 格式化日期
  27. * @param int $value
  28. * @param string $format
  29. * @return Carbon|false|string
  30. */
  31. public static function transformDateTime(int $value, string $format = 'Y-m-d H:i:s')
  32. {
  33. if (!$value) {
  34. return '';
  35. }
  36. $value--;
  37. return date($format, strtotime("1900-01-00 00:00:00 +$value day"));
  38. }
  39. /**
  40. * @param Collection $collection
  41. */
  42. public function collection(Collection $collection)
  43. {
  44. if ($this->readRows == 0) {
  45. //unset($collection[0]);
  46. $this->readRows += count($collection);
  47. }
  48. $this->callback->call($this, $collection);
  49. }
  50. /**
  51. * @return int
  52. */
  53. public function chunkSize(): int
  54. {
  55. return 2000;
  56. }
  57. }