转换为日期时间单元格格式

转换为日期时间单元格格式

本文介绍了PHPExcel setCellValueByColumnAndRow()转换为日期时间单元格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如果我在php变量中有数据时间,有人可以告诉我如何将phpexcel单元格格式设置为日期时间吗?我以这种方式使用它,但是它只是字符串:

Hi can somebody tell me how to set phpexcel cell format to datetime if I have datatime in php variable? I use it this way but it is only string:

$list->setCellValueByColumnAndRow( ++$column, $row, $survey->task->closed->format(DateTimeUtils::DATE_FORMAT_NO_SPACES) );

现在我有了这段代码:

Now I have this code:

$list->setCellValueByColumnAndRow( ++$column, $row, \PHPExcel_Shared_Date::PHPToExcel( $survey->task->closed ) )
->getStyle()->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);

但是它将单元格格式设置为数字,并且日期看起来像浮点数43098.4755671296

but it sets the cell format to number and date seems in result like float number 43098.4755671296

推荐答案

流畅的调用 getStyle()无效,应改为 getStyleByColumnAndRow()在Excel工作表对象上.

It seems the fluent call getStyle() does not work and should be called getStyleByColumnAndRow() instead on excel sheet object.

$activeSheet->getStyleByColumnAndRow( $column, $row, $survey)->getNumberFormat()->setFormatCode( \PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY );

我很困惑,不了解 getStyle() getStyleByColumnAndRow()有什么区别.如果有人知道,请告诉我.

I am confused and don't understand what is the difference between getStyle() and getStyleByColumnAndRow().If somebody knows let me know.

setCellValueByColumnAndRow()方法中有第四个参数确定返回值.流利的getStyle()需要将其设置为TRUE.

There is fourth parameter in setCellValueByColumnAndRow() method which determines return value. Fluent getStyle() needs to set it to TRUE.

这篇关于PHPExcel setCellValueByColumnAndRow()转换为日期时间单元格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:43