PHPExcel How to set a date in cell

后端 未结 2 1082
攒了一身酷
攒了一身酷 2021-01-20 03:28

I need to put a date in a cell, when I take a look to its format it looks like *14/03/01.

The value I put is a simple string and for that reason when I get the calcu

2条回答
  •  孤街浪徒
    2021-01-20 03:37

    MS Excel uses a timestamp value for dates, and then masks it for display purposes; not a formatted string.

    From 02types.php in the /Examples folder:

    $dateTimeNow = time();    //  Get a Unix/PHP timestamp value for the date/time
    $objPHPExcel->getActiveSheet()           // Convert Unix timestamp to a MS Excel 
        ->setCellValue('A9', 'Date/Time')    //  serialized timestamp, and set that as 
        ->setCellValue('B9', 'Date')         //  the cell value
        ->setCellValue(
            'C9', 
            PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow )
        );
    $objPHPExcel->getActiveSheet()        // Format as date and time
        ->getStyle('C9')
        ->getNumberFormat()
        ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
    

    The PHPExcel_Shared_Date::PHPToExcel() method will take a Unix timestamp or a string (formatted like those you might pass to strtotime()) and convert it to a MS Excel timestamp value; while the setFormatCode() calls are setting that cell to a format mask to indicate to MS Excel that the cell contains a value that should be displayed as a date and/or time

提交回复
热议问题