Date and Datetime using PHPExcel function writes in excel as Text

后端 未结 1 1554
时光说笑
时光说笑 2021-01-22 14:57

I have a problem with PHPExcel function, this is my code:



        
相关标签:
1条回答
  • 2021-01-22 15:23

    You need to convert your dates and date/times to an MS Excel serialized datetime stamp.... MS Excel does not automagically convert strings to dates for you, nor does simply setting a style change the actual value (a string in your case) that's stored in the cell in any way.

    PHPExcel provides several different methods in the PHPExcel_Shared_Date class that will let you do these conversions.

    $dto = new DateTime($result[$rowCount][$value]);
    $dateVal = PHPExcel_Shared_Date::PHPToExcel($dto);
    $objPHPExcel->getActiveSheet()->getStyle($column.$cell)
        ->getNumberFormat()
        ->setFormatCode("yyyy.mm.dd h:mm");
    $objPHPExcel->getActiveSheet()->setCellValue($column.$cell, $dateVal);
    
    0 讨论(0)
提交回复
热议问题