PHP |电子表格 |在单元格中设置日期和/或时间值
在PHP电子表格中,日期或时间值以时间戳的形式存储在 Excel 工作表中,时间戳是一个浮点值。因此,为了以人类可读的格式存储日期/时间,需要计算正确的 excel 时间戳,最后设置一个数字格式掩码。
例子:
getActiveSheet();
// Set the number format mask so that the excel timestamp
// will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A1')
->getNumberFormat()
->setFormatCode(
\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
);
// Get current date and timestamp
// Convert to an Excel date/time
$dateTime = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel(
$dateTime );
// Set cell A1 with the Formatted date/time value
$sheet->setCellValue('A1',$excelDateValue);
// Write an .xlsx file
$writer = new Xlsx($spreadsheet);
// Save .xlsx file to the current directory
$writer->save('gfgdate.xlsx');
?>
输出:
参考: https://phpspreadsheet.readthedocs.io/en/develop/topics/accessing-cells/