Laravel-excel: [BUG] κ°€μ Έμ˜€κΈ° λ‚ μ§œ ν˜•μ‹ 문제

에 λ§Œλ“  2018λ…„ 10μ›” 14일  Β·  38μ½”λ©˜νŠΈ  Β·  좜처: Maatwebsite/Laravel-Excel

  • [x] μ½”λ“œ μ™ΈλΆ€μ—μ„œ λ™μž‘μ„ μž¬ν˜„ν•  수 μžˆλŠ” λ¬Έμ œλŠ” Laravel Excel둜 κ²©λ¦¬λ©λ‹ˆλ‹€.
  • [x] λ¬Έμ œκ°€ 아직 μ œμΆœλ˜μ§€ μ•Šμ•˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.
  • [x] 이 문제λ₯Ό ν•΄κ²°ν•˜λŠ” PR이 μ œμΆœλ˜μ§€ μ•Šμ•˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.

버전

  • PHP 버전: 7.1
  • 라라벨 버전: 5.7
  • νŒ¨ν‚€μ§€ 버전: 3.1

μ„€λͺ…

WithChunkReading 및 배치 크기둜 μƒˆ κ°€μ Έμ˜€κΈ°λ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. λ‚΄κ°€ μ§λ©΄ν•œ λ¬Έμ œλŠ” κ°€μ Έμ˜€κΈ° 도ꡬ가 λ‚ μ§œ 열을 νƒ€μž„μŠ€νƒ¬ν”„λ‘œ λ³€ν™˜ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€(λ‚˜λŠ” νƒ€μž„μŠ€νƒ¬ν”„λ₯Ό λ―ΏμŠ΅λ‹ˆλ‹€) 43257.0. 문제λ₯Ό νˆ¬μžν•œ ν›„ μ•„μ£Ό 였래된 μŠ€λ ˆλ“œ https://github.com/Maatwebsite/Laravel-Excel/issues/404λ₯Ό μ°Ύμ•˜κ³  이λ₯Ό ν•΄κ²°ν•œ μ†”λ£¨μ…˜ 쀑 ν•˜λ‚˜λŠ” vendor/maatwebsite/excel/src/Jobs/ReadChunk.php μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” ReadChunk ν΄λž˜μŠ€μ—μ„œ true 값을 false둜 μ„€μ •ν•˜λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€. $this->reader->setReadDataOnly(true);
이 μ†”λ£¨μ…˜μ€ ν˜„μž¬ μž‘λ™ν•˜μ§€λ§Œ μž‘κ³‘κ°€ μ—…λ°μ΄νŠΈλ₯Ό μˆ˜ν–‰ν•˜λ©΄ λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ ꡬ성할 수 μ—†κΈ° λ•Œλ¬Έμ— μ‚¬λΌμ§ˆ κ²ƒμž…λ‹ˆλ‹€.

λ²ˆμ‹ 단계

  1. 엑셀을 λ§Œλ“€λ‹€
  2. λͺ¨λ“  λ‚ μ§œ ν˜•μ‹μœΌλ‘œ 열을 μΆ”κ°€ν•©λ‹ˆλ‹€.
  3. 라이브러리λ₯Ό 톡해 청크 λ©”μ„œλ“œ κ°€μ Έμ˜€κΈ°λ₯Ό μ‚¬μš©ν•˜μ—¬ Excel을 κ°€μ Έμ˜΅λ‹ˆλ‹€.

μ˜ˆμƒλ˜λŠ” λ™μž‘:

λ„μ„œκ΄€μ—μ„œ μ˜ˆμƒλŒ€λ‘œ λ‚ μ§œλ₯Ό μ—…λ‘œλ“œν•  κ²ƒμœΌλ‘œ μ˜ˆμƒν•©λ‹ˆλ‹€.

μ‹€μ œ 행동:

λΌμ΄λΈŒλŸ¬λ¦¬λŠ” λ‚ μ§œλ₯Ό νƒ€μž„μŠ€νƒ¬ν”„λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€(νƒ€μž„μŠ€νƒ¬ν”„ κ°€μ •).

μΆ”κ°€ 정보

μ—¬κΈ° λ‚΄ μˆ˜μž… ν΄λž˜μŠ€κ°€ μžˆμŠ΅λ‹ˆλ‹€.

` λ„€μž„μŠ€νŽ˜μ΄μŠ€ App\Imports;

μ•±\μƒ˜ν”Œ μ‚¬μš©
Maatwebsite\Excel\Concerns\ToModel을 μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.
Maatwebsite\Excel\Concerns\WithBatchInserts μ‚¬μš©;
Maatwebsite\Excel\Concerns\WithChunkReading μ‚¬μš©;
Maatwebsite\Excel\Concerns\WithHeadingRow μ‚¬μš©;
Maatwebsite\Excel\Imports\HeadingRowFormatterλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

HeadingRowFormatter::default('μ—†μŒ');

클래슀 Sample은 ToModel, WithHeadingRow, WithBatchInserts, WithChunkReading을 κ΅¬ν˜„ν•©λ‹ˆλ‹€.
{

public function model(array $row)
{


    return new user([
        'UserName'           => $row['UserName'],
        'Password'           => $row['Password'],
        'date'               => $row['date'],
    ]);
}

public function batchSize(): int
{
    return 1000;
}

public function chunkSize(): int
{
    return 1000;
}

}`

enhancement

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

PR μž‘μ—…μ„ μ‹œμž‘ν–ˆμ§€λ§Œ κ΅¬ν˜„ν•˜κΈ°κ°€ λ³΅μž‘ν•˜λ‹€λŠ” 것을 빨리 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. @patrickbrouwers λŠ” μ½”λ“œλ² μ΄μŠ€λ₯Ό κ²€ν† ν•˜μ—¬ κ°€μž₯ 쒋은 방법이 무엇인지 ν™•μΈν•˜λ €κ³  ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλ™μ•ˆ Import κ°œμ²΄μ—μ„œ λ„μš°λ―Έ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

/**
 * Transform a date value into a Carbon object.
 *
 * <strong i="8">@return</strong> \Carbon\Carbon|null
 */
public function transformDate($value, $format = 'Y-m-d')
{
    try {
        return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
    } catch (\ErrorException $e) {
        return \Carbon\Carbon::createFromFormat($format, $value);
    }
}
class Sample implements ToModel
{
    public function model(array $row)
    {
        return new user([
            'name' => $row[0],
            'email' => $row[1],
            'birth-date' => $this->transformDate($row[2]),
        ]);
    }
}

λͺ¨λ“  38 λŒ“κΈ€

ν…ŒμŠ€νŠΈλ˜μ§€ μ•Šμ•˜μ§€λ§Œ λ‹€μŒμ„ μˆ˜ν–‰ν•΄μ•Ό ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

 return new user([
        'UserName'           => $row['UserName'],
        'Password'           => $row['Password'],
        'date'               => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['date']),
    ]);

setReadDataOnly λŠ” λ‹€λ₯Έ μƒν™©μ—μ„œ 문제λ₯Ό μΌμœΌν‚€κΈ° λ•Œλ¬Έμ— λ‹€μ‹œ μΆ”κ°€ν•˜λŠ” 것이 νŽΈν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ•„λ§ˆλ„ μš°λ¦¬λŠ” 그것을 옡트인 κ΄€μ‹¬μ‚¬λ‘œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” λ˜ν•œμ΄ 문제λ₯Ό κ²½ν—˜ν–ˆμŠ΅λ‹ˆλ‹€. excelToDateTimeObject 이(κ°€) μ‹€μ œ λ‚ μ§œλ₯Ό λ‹€μ‹œ κ°€μ Έμ™”μ§€λ§Œ 이에 λŒ€ν•΄ κ±±μ •ν•˜λŠ” 것이 쒋을 수 μžˆμŠ΅λ‹ˆλ‹€.

μ•„λ§ˆλ„ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

    interface WithDateObjects
    {
        /**
         * An array of columns to convert to DateTime Objects.
         * Either named column headers or coordinate columns.
         *
         * <strong i="8">@return</strong> array
         */
        public function dateColumns(): array;
    }

    class MyImport implements WithDateObjects
    {
        public function dateColumns(): array
        {
            return ['birthday', 'wedding_date'];
        }
    }

그러면 laravel Excel은 κ°€μ Έμ˜€κΈ°κ°€ WithDateObjectsλ₯Ό κ΅¬ν˜„ν•˜λŠ”μ§€ κ°μ§€ν•˜κ³  ν•„μš”μ— 따라 \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($this->cell->getValue()) λ₯Ό μ„€μ •ν•©λ‹ˆκΉŒ?

μΆ”μ‹ : 이것을 Carbon으둜 λ³€ν™˜ν•΄μ•Ό ν•©λ‹ˆκΉŒ?

Carbon λ‚ μ§œλ₯Ό λ°˜ν™˜ν•˜λŠ” WithDates 문제λ₯Ό μΆ”κ°€ν•˜λŠ” PR을 μˆ˜λ½ν•˜κ² μŠ΅λ‹ˆλ‹€. ;)

κΈ€μŽ„, λ‚˜λ„ λ¬Έμ œκ°€μžˆμ–΄, κ·Έ μ „μ—λŠ” 잘 μž‘λ™ν•˜κ³  μ΄μ œλŠ” unexpected data found 였λ₯˜κ°€ λ°œμƒν•˜κ³  이것을 μΆ”κ°€ ν•  λ•Œλ„

\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['date'])

μ—¬μ „νžˆ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

A non well formed numeric value encountered

λ˜ν•œ νƒ„μ†Œλ‘œ λ³€ν™˜ν•˜λ €κ³ ν–ˆμ§€λ§Œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

그러면 $row['date'] λŠ” μ •μˆ˜κ°€ μ•„λ‹™λ‹ˆλ‹€. excelToDateTimeObject λŠ” Excelμ—μ„œ λ‚ μ§œ ν˜•μ‹μ΄ μ§€μ •λœ κ²½μš°μ—λ§Œ μž‘λ™ν•©λ‹ˆλ‹€.

흠 이미 λ‚ μ§œμ— ν¬λ§·λ˜μ—ˆμŠ΅λ‹ˆλ‹€

screen shot 2018-10-25 at 16 48 06

λ‚˜λŠ” "maatwebsite/excel": "^3.1",

디버깅을 μ‹œλ„ν•˜κ³  $row['data'] 무엇이 μžˆλŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€.

WithDates 둜 PR을 보낼 수 μžˆμ§€λ§Œ 적어도 일주일은 걸릴 κ²ƒμž…λ‹ˆλ‹€. λ‚˜λŠ” νšŒμ˜μ— 있고 μ§€κΈˆ 그것에 집쀑할 수 μ—†μŠ΅λ‹ˆλ‹€.

그것은 1/1/18 μž…λ‹ˆλ‹€. mysql ν…Œμ΄λΈ”μ—μ„œ Excel λ‚ μ§œμ—μ„œ λ‚ μ§œλ‘œ μžλ™ λ³€ν™˜λ˜μ–΄μ•Ό ν•©λ‹ˆκΉŒ? 이전에 이 νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν–ˆλŠ”λ° 찾기만 ν•˜λ©΄ μž‘λ™ν•˜μ§€λ§Œ 이제 이 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

1/1/18 λ‹€μ‹œ λ°›μœΌλ©΄ μ •μˆ˜κ°€ μ•„λ‹Œ 것이 λΆ„λͺ…ν•©λ‹ˆλ‹€. λ”°λΌμ„œ excelToDateTimeObject μ‚¬μš©ν•΄μ„œλŠ” μ•ˆλ©λ‹ˆλ‹€. λŒ€μ‹  Carbon::createFromFormat(...) λ₯Ό μ‚¬μš©ν•˜μ—¬ 이λ₯Ό μ²˜λ¦¬ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@patrickbrowers μ•„λ§ˆλ„ withDates λ¬Έμ œλŠ” λ¨Όμ € excelToDateTimeObjectλ₯Ό μ‹œλ„ν•˜κ³  μ˜ˆμ™Έκ°€ λ°œμƒν•˜λ©΄ Carbon::createFromFormat()을 μ‹œλ„ν•˜μ‹­μ‹œμ˜€. dateColumn λ©”μ„œλ“œλŠ” λ‚ μ§œ ν˜•μ‹μ„ λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

public function dateColumns(): array
        {
            return ['birthday' => β€˜d/m/Y’, 'wedding_date'];
        }

μΆ”μ‹ : 이 μ½”λ“œλ₯Ό νœ΄λŒ€ν°μœΌλ‘œ μž‘μ„±ν•˜λ €κ³  ν–ˆμœΌλ‚˜ 백틱을 μˆ˜ν–‰ν•˜λŠ” 방법을 μ•Œ 수 μ—†μŠ΅λ‹ˆλ‹€.

@devinfd 잘 듀리

ps λ°± 틱을 μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€ ;)

@devinfd 당신은 이미 κ΅¬ν˜„μ„ μ–Όλ§ˆλ‚˜ ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? 관심도 μžˆμ„ κ²ƒμž…λ‹ˆλ‹€.

PR μž‘μ—…μ„ μ‹œμž‘ν–ˆμ§€λ§Œ κ΅¬ν˜„ν•˜κΈ°κ°€ λ³΅μž‘ν•˜λ‹€λŠ” 것을 빨리 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. @patrickbrouwers λŠ” μ½”λ“œλ² μ΄μŠ€λ₯Ό κ²€ν† ν•˜μ—¬ κ°€μž₯ 쒋은 방법이 무엇인지 ν™•μΈν•˜λ €κ³  ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλ™μ•ˆ Import κ°œμ²΄μ—μ„œ λ„μš°λ―Έ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

/**
 * Transform a date value into a Carbon object.
 *
 * <strong i="8">@return</strong> \Carbon\Carbon|null
 */
public function transformDate($value, $format = 'Y-m-d')
{
    try {
        return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
    } catch (\ErrorException $e) {
        return \Carbon\Carbon::createFromFormat($format, $value);
    }
}
class Sample implements ToModel
{
    public function model(array $row)
    {
        return new user([
            'name' => $row[0],
            'email' => $row[1],
            'birth-date' => $this->transformDate($row[2]),
        ]);
    }
}

λ˜ν•œ 이것에 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

@devinfd
λ„μš°λ―Έ κΈ°λŠ₯을 μ‚¬μš©ν•˜μ—¬ λ‚ μ§œ ν˜•μ‹μ„ μ§€μ •ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 이 ν˜•μ‹μœΌλ‘œ λ°˜ν™˜λ©λ‹ˆλ‹€. 1943-10-15 00:00:00

μ–΄μ¨Œλ“  10/15/1943 둜 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

μ•„λ‹ˆλ©΄ ν”„λŸ°νŠΈ μ—”λ“œμ—μ„œ μ΄λŸ¬ν•œ μœ ν˜•μ˜ μ„œμ‹μ„ μ²˜λ¦¬ν•˜λŠ” 것이 더 λ‚˜μ„κΉŒμš”?

κΆκΈˆν•©λ‹ˆλ‹€. λˆ„κ΅°κ°€ ....λ‚ μ§œ ν•„λ“œκ°€ 이 μ΄μƒν•œ μ •μˆ˜λ‘œ λŒμ•„μ˜€κΈ°λ₯Ό 원/κΈ°λŒ€/μ‚¬μš© 사둀가 μžˆμŠ΅λ‹ˆκΉŒ? 이 μž‘μ—…μ€ κ°€μ Έμ˜¬ λ•Œ μžλ™μœΌλ‘œ λ°œμƒν•΄μ•Ό ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. Excelμ—μ„œ "2019-01-10"으둜 λ³Ό 수 μžˆλŠ” λ‚ μ§œκ°€ 43473.0으둜 λ‹€μ‹œ λ‚˜μ™€μ•Ό ν•˜λŠ” μ‹œλ‚˜λ¦¬μ˜€λ₯Ό 상상할 수 μ—†μŠ΅λ‹ˆλ‹€.

@nickpoulos λŠ” numberformat이 λ‚ μ§œ ν˜•μ‹μΌ λ•Œ Excelμ—μ„œ μ €μž₯ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€.
PhpSpreadsheetλŠ” 이λ₯Ό datetime 객체둜 λ³€ν™˜ν•˜λ €κ³  μ‹œλ„ν•©λ‹ˆλ‹€(NumberFormat 쀄 649 μ°Έμ‘°). 이것이 μ‹€νŒ¨ν•˜λ©΄ μš°λ¦¬κ°€ ν•  수 μžˆλŠ” 일이 λ§Žμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚ μ§œκ°€ λΉ„μ–΄ μžˆλŠ”μ§€ μ–΄λ–»κ²Œ 확인할 수 μžˆμŠ΅λ‹ˆκΉŒ?

λˆ„κ΅¬λ“ μ§€ 진행 상황 ν‘œμ‹œ μ½”λ“œλ₯Ό κ°€μ Έμ˜€κΈ° Excel νŒŒμΌμ— κ²Œμ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

문제 ν•΄κ²°ν•˜μ‹  λΆ„ κ³„μ‹ κ°€μš”??

문제 ν•΄κ²°ν•˜μ‹  λΆ„ κ³„μ‹ κ°€μš”??

@Jaikangam93
쑰금 λŠ¦μ—ˆμ§€λ§Œ _WithCustomValueBinder_의 λ„μ›€μœΌλ‘œ μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€. λ‚ μ§œ μ—΄μ˜ μ’Œν‘œλ₯Ό μ•½κ°„ ν™•μΈν•˜κ³  PhpOffice\PhpSpreadsheet\Shared\Dateμ—μ„œ excelToDateTimeObject λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κ³  λ§ˆμ§€λ§‰μœΌλ‘œ 셀에 λŒ€ν•œ 값을 λ¬Έμžμ—΄λ‘œ μ„€μ •ν–ˆμŠ΅λ‹ˆλ‹€.

μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

   public function bindValue(Cell $cell, $value)
   {
       if(preg_match('/^E*\d*$/', $cell->getCoordinate())){
                $cell->setValueExplicit(Date::excelToDateTimeObject($value)->format('Y-m-d'), DataType::TYPE_STRING);
        }
        else{
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
        }

        return true;
    }

λͺ¨λ“  λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό κ°€μ Έμ˜€κ³  κ°€μ Έμ˜€κΈ° λ„κ΅¬μ—μ„œ _WithCustomValueBinder_λ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•©λ‹ˆλ‹€.

λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 일뢀 μ…€μ—μ„œ λͺ¨λ“  κ°’(숫자, λ¬Έμžμ—΄, λ‚ μ§œ...)을 κ°€μ§ˆ 수 μžˆλŠ” νŒŒμΌμ„ κ°€μ Έμ˜€κ³  μžˆμŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή μ…€μ˜ μ›λž˜ 값이 λ‚ μ§œμž„μ„ μ–΄λ–»κ²Œ μ•Œ 수 μžˆμŠ΅λ‹ˆκΉŒ? κ·Έλž˜μ„œ ν˜•μ‹μ„ 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. λ°”λ₯΄κ²Œ?

λ‚ μ§œ ν•­λͺ©μ„ 포함할 열을 ꡬ체적으둜 μ•Œκ³  μˆ˜μ • μ‚¬ν•­μ—μ„œ λ™μΌν•˜κ²Œ μ°Έμ‘°ν•΄μ•Ό ν•©λ‹ˆλ‹€. Headingsλ₯Ό μ‚¬μš©ν•˜λŠ” 경우 μ—΄ ν‚€λ‘œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 일반적으둜 λ‚ μ§œ 값을 μ•Œμ•„λ‚΄λŠ” 것은 일반적이면 μ‰¬μšΈ κ²ƒμ΄μ§€λ§Œ Excelμ—μ„œλŠ” λ‚ μ§œλ₯Ό 기본적으둜 λ¬Έμžμ—΄μΈ Excel νƒ€μž„μŠ€νƒ¬ν”„λ‘œ μ €μž₯ν•©λ‹ˆλ‹€. 당신이 ν•  수 μžˆλŠ” 일은 νƒ€μž„μŠ€νƒ¬ν”„μ˜ νŒ¨ν„΄μ„ νŒŒμ•…ν•˜κ³  그에 따라 각 열을 ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μΌμΉ˜ν•˜λŠ” ν•­λͺ©μ΄ 있으면 μ„œμ‹μ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄κ°€ λ³Έ Excel νƒ€μž„ μŠ€νƒ¬ν”„μ—λŠ” 일반적으둜 _ \d*\.\d _ νŒ¨ν„΄μ΄ μžˆμŠ΅λ‹ˆλ‹€. 이 νŒ¨ν„΄μ€ 일련의 숫자 뒀에 점과 λ‹€λ₯Έ μˆ«μžκ°€ μ΄μ–΄μ§‘λ‹ˆλ‹€.

도움이 되기λ₯Ό λ°”λžλ‹ˆλ‹€.

그것은 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€. 이 μ—΄μ˜ λ°μ΄ν„°λŠ” λͺ¨λ“  κ°’ μœ ν˜•(숫자, λ‚ μ§œ, λ¬Έμžμ—΄)을 κ°€μ§ˆ 수 있으며 미리 μ•Œ 수 μ—†μŠ΅λ‹ˆλ‹€. 이 λ°μ΄ν„°λŠ” λ§ˆμΉ¨λ‚΄ json ν•„λ“œμ— λ‘œλ“œλ©λ‹ˆλ‹€. 그리고 그것은 42434.12와 같은 μ™„λ²½ν•˜κ²Œ 값이 될 수 있고 λ‚ μ§œκ°€ 아닐 수 μžˆμŠ΅λ‹ˆλ‹€.

PR μž‘μ—…μ„ μ‹œμž‘ν–ˆμ§€λ§Œ κ΅¬ν˜„ν•˜κΈ°κ°€ λ³΅μž‘ν•˜λ‹€λŠ” 것을 빨리 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. @patrickbrouwers λŠ” μ½”λ“œλ² μ΄μŠ€λ₯Ό κ²€ν† ν•˜μ—¬ κ°€μž₯ 쒋은 방법이 무엇인지 ν™•μΈν•˜λ €κ³  ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλ™μ•ˆ Import κ°œμ²΄μ—μ„œ λ„μš°λ―Έ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

/**
 * Transform a date value into a Carbon object.
 *
 * <strong i="9">@return</strong> \Carbon\Carbon|null
 */
public function transformDate($value, $format = 'Y-m-d')
{
    try {
        return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
    } catch (\ErrorException $e) {
        return \Carbon\Carbon::createFromFormat($format, $value);
    }
}
class Sample implements ToModel
{
    public function model(array $row)
    {
        return new user([
            'name' => $row[0],
            'email' => $row[1],
            'birth-date' => $this->transformDate($row[2]),
        ]);
    }
}

맀λ ₯처럼 μž‘λ™ν•©λ‹ˆλ‹€. 감사 ν•΄μš”

@in33t @patrickbrouwers
빈 ν•„λ“œλ₯Ό λ¨Όμ € ν™•μΈν•˜μ‹­μ‹œμ˜€(try / catch 전에). 그렇지 μ•ŠμœΌλ©΄ ν•¨μˆ˜κ°€ λΉ„μ–΄ 있으면 λ‚ μ§œ 1970-01-01을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

if(!strlen($value)) return null;

μ•ˆλ…•ν•˜μ„Έμš”, 이 μ†”λ£¨μ…˜μ„ μ°Ύμ•˜μŠ΅λ‹ˆλ‹€. μ €μ—κ²Œ νš¨κ³Όμ μž…λ‹ˆλ‹€!

protected function formatDateExcel($date){ if (gettype($date) === 'double') { $birthday = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($date); return $birthday->format('n/j/Y'); } return $date; }

그리고 λ‚˜λŠ” μ‚¬μš©

$this->formatDateExcel($row['birthday']);

λˆ„κ΅¬λ‚˜ m/d/Y ν˜•μ‹κ³Ό 같이 04/04/1977 λ₯Ό μ²˜λ¦¬ν•˜λŠ” 방법을 μ„€λͺ…ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

Date::excelToDateTimeObject($row['birth_date'])

λ‚΄κ°€ λ³Ό λ•Œ ν˜•μ‹ 인수λ₯Ό ν—ˆμš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆκΉŒ?

데λͺ¨ λͺ©μ :

'birthday' => $row['birth_date'] ? Date::excelToDateTimeObject($row['birth_date']) : null,

그리고 λ¬Όλ‘  μš°λ¦¬λŠ” 04/04/1977 0000-00-00.... μ–»μ—ˆμŠ΅λ‹ˆλ‹€. μ™œλƒν•˜λ©΄ μ–΄λŠ 것이 월이고 μ–΄λŠ 것이 μš”μΌμΈμ§€ λͺ¨λ₯΄κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€ :man_facepalming:

μ—…λ°μ΄νŠΈλ¨ :
Nevermind λ¬Έμ œλŠ” 생일이 1970λ…„ 미만인 μ‚¬λžŒλ“€μ—κ²Œλ§Œ ν•΄λ‹Ή

두 번째 νŽΈμ§‘ 은 μ—΄ μœ ν˜•μ„ timestamp μ—μ„œ dateTime - μΌ€μ΄μŠ€κ°€ μ’…λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

λ‚ μ§œ ν•„λ“œκ°€ μžˆλŠ” Excel νŒŒμΌμ„ κ°€μ Έμ˜€κ³  μ‹Άμ§€λ§Œ 1992/03/01 ν˜•μ‹μ΄ 1992-03-01둜 λ§Œλ“€μ–΄μ§€λŠ” 사이에 였λ₯˜κ°€ λ°œμƒν•˜κ±°λ‚˜ 이λ₯Ό 극볡할 수 μžˆλŠ” λ‹€λ₯Έ 방법이 μžˆμŠ΅λ‹ˆκΉŒ?
이 문제λ₯Ό 극볡할 수 μžˆλ„λ‘ λ„μ™€μ£Όμ„Έμš”.

λ‚ μ§œ ν•„λ“œκ°€ μžˆλŠ” Excel νŒŒμΌμ„ κ°€μ Έμ˜€κ³  μ‹Άμ§€λ§Œ 1992/03/01 ν˜•μ‹μ΄ 1992-03-01둜 λ§Œλ“€μ–΄μ§€λŠ” 사이에 였λ₯˜κ°€ λ°œμƒν•˜κ±°λ‚˜ 이λ₯Ό 극볡할 수 μžˆλŠ” λ‹€λ₯Έ 방법이 μžˆμŠ΅λ‹ˆκΉŒ?
이 문제λ₯Ό 극볡할 수 μžˆλ„λ‘ λ„μ™€μ£Όμ„Έμš”.

μ•ˆλ…•ν•˜μ„Έμš” nizardaniλ‹˜
Excel νŒŒμΌμ—μ„œ λ‚ μ§œ ν•„λ“œ 1992-03-01의 μ—΄ νŽΈμ§‘μ—μ„œ λ‹€μŒκ³Ό 같이 μ‹œλ„ν•˜μ‹­μ‹œμ˜€.
이 λ‚ μ§œ 1992-03-01은 Laravel Excelμ—μ„œ ν—ˆμš©ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ—΄ ν•„λ“œ μœ ν˜•μ„ μ—…λ°μ΄νŠΈ/λ³€κ²½ν•΄μ•Ό ν•©λ‹ˆλ‹€.
ν•΄κ²° 단계 :-
1) κ°€μ Έμ˜¬ μ—‘μ…€ νŒŒμΌμ„ μ—½λ‹ˆλ‹€. λ‚ μ§œ μ—΄ 선택 마우슀 였λ₯Έμͺ½ λ²„νŠΌμ„ ν΄λ¦­ν•˜κ³  μ…€ μ„œμ‹ 선택
2) μ…€ μ„œμ‹ > 숫자 > μ‚¬μš©μž μ •μ˜ > μœ ν˜• > dd-mm-yyyy hh:mm:ss
μƒ˜ν”Œμ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. > 03-06-2018 00:00:00
μ„œμ‹ 셀을 λ³€κ²½ν•œ ν›„. 2018λ…„ 3μ›” 6일과 같이 ν‘œμ‹œλ˜λŠ” 경우.
03-06-2018 00:00:00κ³Ό 같이 λ³€κ²½λ˜λ„λ‘ 두 번 ν΄λ¦­ν•˜μ‹­μ‹œμ˜€.

λ‹€μ‹œ 파일 κ°€μ Έμ˜€κΈ°.... 이제 μž‘λ™ν•˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.

λ‚ μ§œ ν•„λ“œκ°€ μžˆλŠ” Excel νŒŒμΌμ„ κ°€μ Έμ˜€κ³  μ‹Άμ§€λ§Œ 1992/03/01 ν˜•μ‹μ΄ 1992-03-01둜 λ§Œλ“€μ–΄μ§€λŠ” 사이에 였λ₯˜κ°€ λ°œμƒν•˜κ±°λ‚˜ 이λ₯Ό 극볡할 수 μžˆλŠ” λ‹€λ₯Έ 방법이 μžˆμŠ΅λ‹ˆκΉŒ?
이 문제λ₯Ό 극볡할 수 μžˆλ„λ‘ λ„μ™€μ£Όμ„Έμš”.

μ•ˆλ…•ν•˜μ„Έμš” nizardaniλ‹˜
Excel νŒŒμΌμ—μ„œ λ‚ μ§œ ν•„λ“œ 1992-03-01의 μ—΄ νŽΈμ§‘μ—μ„œ λ‹€μŒκ³Ό 같이 μ‹œλ„ν•˜μ‹­μ‹œμ˜€.
이 λ‚ μ§œ 1992-03-01은 Laravel Excelμ—μ„œ ν—ˆμš©ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ—΄ ν•„λ“œ μœ ν˜•μ„ μ—…λ°μ΄νŠΈ/λ³€κ²½ν•΄μ•Ό ν•©λ‹ˆλ‹€.
ν•΄κ²° 단계 :-

  1. κ°€μ Έμ˜¬ μ—‘μ…€ νŒŒμΌμ„ μ—½λ‹ˆλ‹€. λ‚ μ§œ μ—΄ 선택 마우슀 였λ₯Έμͺ½ λ²„νŠΌμ„ ν΄λ¦­ν•˜κ³  μ…€ μ„œμ‹ 선택
  2. μ…€ μ„œμ‹> 숫자 > μ‚¬μš©μž μ •μ˜> μœ ν˜•> dd-mm-yyyy hh:mm:ss
    μƒ˜ν”Œμ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. > 03-06-2018 00:00:00
    μ„œμ‹ 셀을 λ³€κ²½ν•œ ν›„. 2018λ…„ 3μ›” 6일과 같이 ν‘œμ‹œλ˜λŠ” 경우.
    03-06-2018 00:00:00κ³Ό 같이 λ³€κ²½λ˜λ„λ‘ 두 번 ν΄λ¦­ν•˜μ‹­μ‹œμ˜€.

λ‹€μ‹œ 파일 κ°€μ Έμ˜€κΈ°.... 이제 μž‘λ™ν•˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.

15788022402754767278257596482531
μ—‘μ…€μ—μ„œ λ‚ μ§œ νƒ€μž…μ„ λ³€κ²½ν–ˆλŠ”λ° λ‹€μ‹œ λ¬Έμ œκ°€ λ°œμƒν•˜λŠ”λ° μΉ΄λ³Έ λ„€μŠ€λ΄‡μ€ 무엇을 μ‚¬μš©ν•΄μ•Ό ν•˜λ‚˜μš”?

κ·Έλž˜μ„œ λ‚ μ§œ λ˜λŠ” datetime 개체둜 λ³€ν™˜ν•΄μ•Ό ν•˜λŠ”μ§€ μ—¬λΆ€λ₯Ό κ²°μ •ν•˜κΈ° 전에 셀이 λ‚ μ§œ ν˜•μ‹μΈμ§€ μ•Œλ €μ£ΌλŠ” 방법을 μ•Œμ•„λ‚Έ μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ? μŠ€ν”„λ ˆλ“œμ‹œνŠΈκ°€ Excel에 λ‹€μ‹œ λ‘œλ“œλ  λ•Œ μ €μž₯된 λ‚ μ§œ ν•„λ“œκ°€ λ‚ μ§œ ν•„λ“œλ‘œ λ°˜ν™˜λ˜κΈ° λ•Œλ¬Έμ— Excel은 λΆ„λͺ…νžˆ μ•Œκ³  μžˆμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ λ‚ μ§œκ°€ ν¬ν•¨λœ μ…€μž„μ„ μ•Œ 수 μžˆλŠ” ν•΄λ‹Ή 셀에 λŒ€ν•œ 일뢀 메타데이터가 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€ .

κ°„λ‹¨νžˆ μ‚΄νŽ΄λ³΄λ‹ˆ μ›Œν¬μ‹œνŠΈμ— λ‹€μŒκ³Ό 같이 λ‚˜μ™€ μžˆμŠ΅λ‹ˆλ‹€.

<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" mc:Ignorable="x14ac xr xr2 xr3" xr:uid="{A7382266-55D2-4E6B-95C9-90E61705D12D}">
   ...
   <sheetData>
      <row r="4" spans="1:1" x14ac:dyDescent="0.25">
         <c r="A4">
            <v>54321</v>
         </c>
      </row>
      <row r="5" spans="1:1" x14ac:dyDescent="0.25">
         <c r="A5" s="1">
            <v>43924</v>
         </c>
      </row>
   </sheetData>
   ...
</worksheet>

μ…€ A4λŠ” μ •μˆ˜μ΄κ³  μ…€ A5λŠ” λ‚ μ§œμž…λ‹ˆλ‹€. λ‹€λ₯Έ 점은 s="1" μ†μ„±μž…λ‹ˆλ‹€. 그게 λ°μ΄νŠΈκ°€ λœλ‹€. Book1.xlsx λ₯Ό νŽΈμ§‘ν•˜μ—¬ 이 속성을 A4 셀에 μΆ”κ°€ν•˜λ©΄ μŠ€ν”„λ ˆλ“œμ‹œνŠΈλ₯Ό μ—΄λ©΄ 두 개의 λ‚ μ§œκ°€ ν‘œμ‹œλ©λ‹ˆλ‹€. 54321은 2048λ…„ 9μ›” 20μΌμž…λ‹ˆλ‹€. κΆκΈˆν•˜μ‹  뢄듀을 μœ„ν•΄ ;-)

λ”°λΌμ„œ λ°μ΄ν„°λŠ” 이 μˆ«μžκ°€ μˆ«μžκ°€ μ•„λ‹ˆλΌ λ‚ μ§œμž„μ„ 이 λΌμ΄λΈŒλŸ¬λ¦¬μ— μ•Œλ¦¬κΈ° μœ„ν•΄ μŠ€ν”„λ ˆλ“œμ‹œνŠΈμ— μžˆμŠ΅λ‹ˆλ‹€. μ‚¬μ–‘μ—μ„œ 이λ₯Ό 찾아보지 μ•Šμ•˜κΈ° λ•Œλ¬Έμ— 그보닀 더 λ―Έλ¬˜ν•œ 뢀뢄이 μžˆμ„ κ²ƒμœΌλ‘œ μ˜ˆμƒν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ μš”μ μ€ μŠ€ν”„λ ˆλ“œμ‹œνŠΈλ₯Ό κ°€μ Έμ˜€λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ 데이터 μœ ν˜•μ΄ 무엇인지 미리 μ•Œ ν•„μš”κ°€ μ—†λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

μ—…λ°μ΄νŠΈ : μ—¬κΈ° 정말 쒋은 λŒ€λ‹΅ s=1 μ—μ„œ λ¦¬λ””λ ‰μ…˜ μ°Έμ‘° λͺ‡ 톡과 styles.xml ꢁ극적으둜 λ‹Ήμ‹ μ—κ²Œ λ‚΄λΆ€ ν˜•μ‹ λ˜λŠ” μ‚¬μš©μž μ •μ˜ ν˜•μ‹μ„ μ œκ³΅ν•©λ‹ˆλ‹€. λ‚˜λŠ” ν‘œμ‹œ ν˜•μ‹μ΄ μˆ«μžκ°€ λ‚ μ§œκ°€ λ˜λ„λ‘ μ˜λ„ ν–ˆλŠ”μ§€ μ—¬λΆ€λ₯Ό κ²°μ •ν•΄μ•Ό ν•˜λŠ” 전뢀라고 μƒκ°ν•©λ‹ˆλ‹€. λ‚΄λΆ€ ν˜•μ‹ 쀑 μΌλΆ€λŠ” ν•˜λ“œ μ½”λ”©ν•˜κΈ°μ— μΆ©λΆ„νžˆ μ‰½μ§€λ§Œ μ‚¬μš©μž 지정 ν˜•μ‹μ€ λ””μ½”λ”©ν•˜κΈ°κ°€ 쑰금 더 μ–΄λ ΅μŠ΅λ‹ˆλ‹€.

PhpSpreadsheetλŠ” 이 ν˜•μ‹μ— λŒ€ν•΄ μ•Œκ³  μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ„±λŠ₯μƒμ˜ 이유둜 Excel νŒŒμΌμ„ read_only λͺ¨λ“œλ‘œ μ½λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ—μ„œ λΉ„ν™œμ„±ν™”ν•  수 μžˆλŠ” 경우: https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/config/excel.php#L47 PhpSpreadsheetλŠ” μžλ™μœΌλ‘œ λ‚ μ§œλ₯Ό λ‚ μ§œλ‘œ 읽어야 ν•©λ‹ˆλ‹€. 더 μ„±λŠ₯이 쒋은 섀정을 μ„ ν˜Έν•˜λŠ” 경우 PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($date) μ‚¬μš©ν•˜μ—¬ λ‚ μ§œ ν˜•μ‹μ„ 직접 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.

ν…ŒμŠ€νŠΈλ˜μ§€ μ•Šμ•˜μ§€λ§Œ λ‹€μŒμ„ μˆ˜ν–‰ν•΄μ•Ό ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

 return new user([
        'UserName'           => $row['UserName'],
        'Password'           => $row['Password'],
        'date'               => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['date']),
    ]);

setReadDataOnly λŠ” λ‹€λ₯Έ μƒν™©μ—μ„œ 문제λ₯Ό μΌμœΌν‚€κΈ° λ•Œλ¬Έμ— λ‹€μ‹œ μΆ”κ°€ν•˜λŠ” 것이 νŽΈν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ•„λ§ˆλ„ μš°λ¦¬λŠ” 그것을 옡트인 κ΄€μ‹¬μ‚¬λ‘œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

μŠ¬ν”„κ²Œλ„ 이 방법은 λ‚΄ λ‚ μ§œλ₯Ό 1900-01-08둜 λ³€κ²½ν•©λ‹ˆλ‹€.

ν•„μš”μ— 따라 λ‚ μ§œ/μ‹œκ°„ ν•„λ“œλ₯Ό λ™μ μœΌλ‘œ ν•΄κ²°ν•΄μ•Ό ν•˜λŠ” 경우 값이 λ™μ μœΌλ‘œ λ‚ μ§œ/μ‹œκ°„μΈμ§€ μ—¬λΆ€λ₯Ό μžλ™μœΌλ‘œ κ°μ§€ν•˜λŠ” λ©”μ„œλ“œλ₯Ό μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€(ν•΄λ‹Ή 열에 λ‚ μ§œ/μ‹œκ°„μ΄ μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό μ•Œ 수 μžˆλŠ”μ§€ 여뢀에 관계없이) λ˜λŠ” λ‹€μ–‘ν•œ μ‹œλ„ 데이터 μœ ν˜• 및 잘 μž‘λ™

       /**
     * <strong i="6">@param</strong> Cell $cell
     * <strong i="7">@param</strong> $value
     * 
     * <strong i="8">@return</strong> boolean;
     */
    public function bindValue(Cell $cell, $value)
    {
        $formatedCellValue = $this->formatDateTimeCell($value, $datetime_output_format = "d-m-Y H:i:s", $date_output_format = "d-m-Y", $time_output_format = "H:i:s" );
        if($formatedCellValue != false){
            $cell->setValueExplicit($formatedCellValue, DataType::TYPE_STRING);
            return true;
        }

        // else return default behavior
        return parent::bindValue($cell, $value);
    }


    /**
     * 
     * Convert excel-timestamp to Php-timestamp and again to excel-timestamp to compare both compare
     * By Leonardo J. Jauregui ( <strong i="9">@Nanod10</strong> | siskit dot com )
     * 
     * <strong i="10">@param</strong> $value (cell value)
     * <strong i="11">@param</strong> String $datetime_output_format
     * <strong i="12">@param</strong> String $date_output_format
     * <strong i="13">@param</strong> String $time_output_format
     * 
     * <strong i="14">@return</strong> $formatedCellValue
     */
    private function formatDateTimeCell( $value, $datetime_output_format = "Y-m-d H:i:s", $date_output_format = "Y-m-d", $time_output_format = "H:i:s" )
    {

        // is only time flag
        $is_only_time = false;

        // Divide Excel-timestamp to know if is Only Date, Only Time or both of them
        $excel_datetime_exploded = explode(".", $value);

        // if has dot, maybe date has time or is only time
        if(strstr($value,".")){
            // Excel-timestamp to Php-DateTimeObject
            $dateTimeObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value);
            // if Excel-timestamp > 0 then has Date and Time 
            if(intval($excel_datetime_exploded[0]) > 0){
                // Date and Time
                $output_format = $datetime_output_format;
                $is_only_time = false;
            }else{
                // Only time
                $output_format = $time_output_format;
                $is_only_time = true;
            }
        }else{
            // Only Date
            // Excel-timestamp to Php-DateTimeObject
            $dateTimeObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value);
            $output_format = $date_output_format;
            $is_only_time = false;
        }

        // Php-DateTimeObject to Php-timestamp
        $phpTimestamp = $dateTimeObject->getTimestamp();

        // Php-timestamp to Excel-timestamp
        $excelTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $phpTimestamp );

        // if is only Time
        if($is_only_time){
            // 01-01-1970 = 25569
            // Substract to match PhpToExcel conversion
            $excelTimestamp = $excelTimestamp - 25569;
        }

        /* 
        // uncoment to debug manualy and see if working
        $debug_arr = [
                "value"=>$value,
                "value_float"=>floatval($value),
                "dateTimeObject"=>$dateTimeObject,
                "phpTimestamp"=>$phpTimestamp,
                "excelTimestamp"=>$excelTimestamp,
                "default_date_format"=>$dateTimeObject->format('Y-m-d H:i:s'),
                "custom_date_format"=>$dateTimeObject->format($output_format)
            ];

        if($cell->getColumn()=="Q"){
            if($cell->getRow()=="2"){
                if(floatval($value)===$excelTimestamp){
                    dd($debug_arr);
                }
            }
        }

        */

        // if the values match
        if( floatval($value) === $excelTimestamp ){
            // is a fucking date! ;)
            $formatedCellValue = $dateTimeObject->format($output_format);
            return $formatedCellValue;
        }else{
            // return normal value
            return false;
        }

    }

μ•ˆλ…•ν•˜μ„Έμš”. 같은 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. ToCollection μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

public function collection(Collection $collection) λ©”μ†Œλ“œλŠ” ok둜 μ„ μ–Έλ˜κ³  호좜되며 λͺ¨λ“  μ²˜λ¦¬κ°€ μžˆμŠ΅λ‹ˆλ‹€.

문제점 : collection() λ©”μ†Œλ“œμ—μ„œ ν•˜λ‚˜μ˜ datetime 셀이 datetime λ¬Έμžμ—΄ λŒ€μ‹  숫자둜 λ³€ν™˜λ©λ‹ˆλ‹€. 이 λ³€ν™˜μ„ λΉ„ν™œμ„±ν™”ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό ν•©λ‹ˆκΉŒ? κ°’ ν˜•μ‹μ„ μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€. 그리고 μ–΄μ¨Œλ“  XLS νŒŒμΌμ„ μˆ˜μ •ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

class MyImport implements WithCustomValueBinder, ToCollection
β€” bindValues κ°€ collection() 전에 ν˜ΈμΆœλ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ €μ—κ²Œλ„ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

ToModel 이미 κ΅¬ν˜„λœ μ»¬λ ‰μ…˜ ()으둜 μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. 그리고 XLS의 ν˜•μ‹μ€ μ„ ν˜•μ΄ μ•„λ‹™λ‹ˆλ‹€. 맞좀 μ²˜λ¦¬κ°€ ν•„μš”ν•©λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”, 여기에 λΉ„μŠ·ν•œ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. laravel의 μœ νš¨μ„± 검사기λ₯Ό μ‚¬μš©ν•˜μ—¬ νŠΉμ • λ‚ μ§œ ν˜•μ‹μ„ ν™•μΈν•˜κ³  λ‹€λ₯Έ λ‚ μ§œ ν•„λ“œ μ•žμ— μžˆλŠ”μ§€ ν™•μΈν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. 이것은 μ–΄λ–»κ²Œ λ“  κ°€λŠ₯ν•©λ‹ˆκΉŒ? μ§€κΈˆκΉŒμ§€ 찾은 μœ μΌν•œ μ†”λ£¨μ…˜μ€ λ‚ μ§œ μ‹œκ°„ 셀을 xlsx νŒŒμΌμ— λ¬Έμžμ—΄λ‘œ μ €μž₯ν•˜κ³  λ‹€μŒκ³Ό 같이 μœ νš¨μ„± 검사기λ₯Ό μ‹€ν–‰ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

public function rules(): array
    {
        return [
            'start' => 'required|date_format:d/m/y H:i|before:*.end',
            'end' => 'required|date_format:d/m/y H:i|after:*.end',
        ];
    }

예, λ˜ν•œ μ…€ ν˜•μ‹μ„ μˆ˜λ™μœΌλ‘œ λ¬Έμžμ—΄λ‘œ λ³€κ²½ν•œ λ‹€μŒ 확인을 ꡬ문 λΆ„μ„ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ…€ ν˜•μ‹ Dateteme으둜 μ •κΈ°μ μœΌλ‘œ μŠ€νŠΈλ¦¬λ°λ˜λ―€λ‘œ 항상 xls νŒŒμΌμ„ μˆ˜μ •ν•  μˆ˜λŠ” μ—†μŠ΅λ‹ˆλ‹€.

λ¬Έμ œκ°€ μ—¬μ „νžˆ μ§€μ†λ©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰