Laravel-Excel: [BUG] import date format issue
- Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
- Checked that your issue isn’t already filed.
- Checked if no PR was submitted that fixes this problem.
Versions
- PHP version: 7.1
- Laravel version: 5.7
- Package version: 3.1
Description
I created new import with WithChunkReading and batch size. The issue i am facing is importer converts date columns to timestamp( i believe its timestamp) 43257.0. after investing the issue i found very old thread https://github.com/Maatwebsite/Laravel-Excel/issues/404 and one of the solution that fixed it for me was setting true value to false in class ReadChunk available at vendor/maatwebsite/excel/src/Jobs/ReadChunk.php
. line is $this->reader->setReadDataOnly(true);
This solution works for now but when we will do composer update it will be gone as its not configurable in library.
Steps to Reproduce
- create excel
- add column with any date format.
- import excel using chunk method importer via library.
Expected behavior:
I would expect the library to upload the date as expected.
Actual behavior:
library converts date to timestamp( assuming its timestamp)
Additional Information
Here is my import class,
`<?php namespace App\Imports;
use App\Sample; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithBatchInserts; use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default(‘none’);
class Sample implements 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;
}
}`
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 42 (13 by maintainers)
Untested, but I believe you need to do:
I remember
setReadDataOnly
to cause problems in some other situations, so I’m not comfortable just adding that again. Perhaps we can make it an opt-in concern or something.I started working on a PR but quickly realized that it was going to be complicated to implement. @patrickbrouwers was going to review the codebase to see what the best way forward would be.
In the mean time I am just using a helper method on my Import object:
hmmm it already formatted on date
i am using
"maatwebsite/excel": "^3.1",
Hi, i find this solution, works for me!!!
and i use
$this->formatDateExcel($row['birthday']);
I can send a PR with
WithDates
but it will be a least a week. I am at a conference and cant focus on that right now.Works like a Charm. Thank You
PhpSpreadsheet does know about this format. However for performance reasons it’s recommend to read the Excel file in read_only mode. If you can disable that here: https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/config/excel.php#L47 PhpSpreadsheet should then read the date as date automatically. If you prefer the more performant setting, you can format the dates yourself using the
PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($date)
@Jaikangam93 A little late, but I happened to fix it with the help of WithCustomValueBinder wrote a little check for the coordinate of date column and used excelToDateTimeObject method on PhpOffice\PhpSpreadsheet\Shared\Date and finally set the value as for the cell as string.
Here is the code:
Make sure to import all the namespaces and implement WithCustomValueBinder on the importer.
Anybody can post the code of progress status show, on import excel file.
I have created a function to check the file extensions. which parse date as per the file extension
Controller code
Inside the
FileImportRequest
Class.Inside the
FileImport
class.protected $extension;
I have a problem, I’m importing a file that in some cells can have any value (numeric, string, date…), how can I know that the original value in that cell was a date, so I can format it correctly?