Laravel excel maatwebsite 3.1 导入,excel 单元格中的日期列返回为未知格式数字。如何解决这个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55390456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:18:48  来源:igfitidea点击:

Laravel excel maatwebsite 3.1 import, date column in excel cell returns as unknown format number. How to solve this?

phplaravelmaatwebsite-excel

提问by Reno Anthus

By using Maatwebsite/Laravel-Excel version 3.1 to import excel sheet, here I faced an issue date time column of the excel sheet returns unknown number. How to solve this? Example : Consider Cell value "29/07/1989" and returns as "32178" when import.

通过使用Maatwebsite/Laravel-Excel 3.1版导入excel表,这里我遇到了excel表的日期时间列返回未知数字的问题。如何解决这个问题?示例:考虑单元格值“29/07/1989”并在导入时返回“32178”。

回答by Qraxin

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

这些数字来自 excel 本身,日期作为数值存储在 excel 中。http://www.cpearson.com/excel/datetime.htm

对于 Laravel 框架 5.6 和 maatwebsite/excel 包版本 3.1,将日期从 excel 数字转换为普通日期格式,PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($dateFromExcel)可以使用此功能 。它接受整数(excel 日期)并返回 DateTime 对象。

更多信息可以在这里找到 https://github.com/Maatwebsite/Laravel-Excel/issues/1832

From this answer: https://stackoverflow.com/a/55139981/9133724

从这个答案:https: //stackoverflow.com/a/55139981/9133724

回答by Reno Anthus

Solved! This is the code I used to solve my problem:

解决了!这是我用来解决问题的代码:

Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));