Laravel Excel is converting dates from heading into some numbers

浪子不回头ぞ 提交于 2019-12-11 07:31:47

问题


I have a problem which is connected with Laravel Excel. I have heading filled with dates for example: 2018-05-23. But when I read it with excel extension by using:

 $data = Excel::load($request['file'], function($reader) {})->get();

The extension is not treating the heading dates as dates. Instead of that it converts the dates into some numbers - for example: 43243.

Here is dd of the sheet:

Important note: When the date is in a normal row (not in heading) then the date is converted well.


Version of Laravel Excel (maatwebsite/excel) - 2.1.0
Version of Laravel - 5.5


回答1:


I think due to ExcelParser. It converts the date header field into some random numbers.

So to make it work for your requirement then add the column header inside the double quotes.

Example : "2018-05-23"

https://github.com/Maatwebsite/Laravel-Excel/blob/2.1/src/Maatwebsite/Excel/Parsers/ExcelParser.php#L284




回答2:


The numbers come from excel itself, dates stored in excel as numeric values. http://www.cpearson.com/excel/datetime.htm

For Laravel framework 5.6 and maatwebsite/excel package version 3.1, to convert date from excel numbers to normal date format, this function PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($dateFromExcel) can be used. It accepts integer(excel date) and returns DateTime object.

More information can be found here https://github.com/Maatwebsite/Laravel-Excel/issues/1832



来源:https://stackoverflow.com/questions/50481950/laravel-excel-is-converting-dates-from-heading-into-some-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!