PHPExcel setCellValueByColumnAndRow() to datetime cell format

烈酒焚心 提交于 2019-12-24 09:36:43

问题


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) );

EDIT: 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);

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


回答1:


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 );

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

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



来源:https://stackoverflow.com/questions/48518427/phpexcel-setcellvaluebycolumnandrow-to-datetime-cell-format

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