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);
μ΄ μ루μ
μ νμ¬ μλνμ§λ§ μκ³‘κ° μ
λ°μ΄νΈλ₯Ό μννλ©΄ λΌμ΄λΈλ¬λ¦¬μμ ꡬμ±ν μ μκΈ° λλ¬Έμ μ¬λΌμ§ κ²μ
λλ€.
μμλλ λμ:
λμκ΄μμ μμλλ‘ λ μ§λ₯Ό μ λ‘λν κ²μΌλ‘ μμν©λλ€.
μ€μ νλ:
λΌμ΄λΈλ¬λ¦¬λ λ μ§λ₯Ό νμμ€ν¬νλ‘ λ³νν©λλ€(νμμ€ν¬ν κ°μ ).
μ¬κΈ° λ΄ μμ ν΄λμ€κ° μμ΅λλ€.
` λ€μμ€νμ΄μ€ 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;
}
}`
ν μ€νΈλμ§ μμμ§λ§ λ€μμ μνν΄μΌ νλ€κ³ μκ°ν©λλ€.
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μμ λ μ§ νμμ΄ μ§μ λ κ²½μ°μλ§ μλν©λλ€.
ν μ΄λ―Έ λ μ§μ ν¬λ§·λμμ΅λλ€
λλ "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μμ νμ©νμ§ μμΌλ―λ‘ μ΄ νλ μ νμ μ λ°μ΄νΈ/λ³κ²½ν΄μΌ ν©λλ€.
ν΄κ²° λ¨κ³ :-
- κ°μ Έμ¬ μμ νμΌμ μ½λλ€. λ μ§ μ΄ μ ν λ§μ°μ€ μ€λ₯Έμͺ½ λ²νΌμ ν΄λ¦νκ³ μ μμ μ ν
- μ μμ> μ«μ > μ¬μ©μ μ μ> μ ν> dd-mm-yyyy hh:mm:ss
μνμ λ€μκ³Ό κ°μ΅λλ€. > 03-06-2018 00:00:00
μμ μ μ λ³κ²½ν ν. 2018λ 3μ 6μΌκ³Ό κ°μ΄ νμλλ κ²½μ°.
03-06-2018 00:00:00κ³Ό κ°μ΄ λ³κ²½λλλ‘ λ λ² ν΄λ¦νμμμ€.λ€μ νμΌ κ°μ Έμ€κΈ°.... μ΄μ μλνκΈ°λ₯Ό λ°λλλ€.
μμ
μμ λ μ§ νμ
μ λ³κ²½νλλ° λ€μ λ¬Έμ κ° λ°μνλλ° μΉ΄λ³Έ λ€μ€λ΄μ 무μμ μ¬μ©ν΄μΌ νλμ?
κ·Έλμ λ μ§ λλ 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 νμΌμ μμ ν μλ μμ΅λλ€.
λ¬Έμ κ° μ¬μ ν μ§μλ©λλ€.
κ°μ₯ μ μ©ν λκΈ
PR μμ μ μμνμ§λ§ ꡬννκΈ°κ° λ³΅μ‘νλ€λ κ²μ 빨리 κΉ¨λ¬μμ΅λλ€. @patrickbrouwers λ μ½λλ² μ΄μ€λ₯Ό κ²ν νμ¬ κ°μ₯ μ’μ λ°©λ²μ΄ 무μμΈμ§ νμΈνλ €κ³ νμ΅λλ€.
κ·Έλμ Import κ°μ²΄μμ λμ°λ―Έ λ©μλλ₯Ό μ¬μ©νκ³ μμ΅λλ€.