我需要在单元格中输入日期,但当我查看其格式时,它看起来像*14/03/01。
我输入的值是一个简单的字符串,因此当我获取计算出的值时,会忽略我输入的日期,因为它会在日历(实际年份的所有日期所在的列)中将我输入的日期与日期进行比较,以设置相应日期的正确值。
有没有办法输入Excel期望的格式?
MS Excel使用时间戳来表示日期,并且为了显示目的进行掩码处理,而不是格式化字符串。
来自于/Examples
文件夹中的02types.php
:
$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);
PHPExcel_Shared_Date::PHPToExcel()
方法将接受 Unix 时间戳或字符串(格式类似于您可能传递给 strtotime()
的内容),并将其转换为 MS Excel 时间戳值;而 setFormatCode()
调用则会将该单元格设置为格式掩码,以提示 MS Excel 该单元格包含应被显示为日期和/或时间的值。
$duree = '08:00:00';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$sheet->setCellValueByColumnAndRow($row, $num, $duree);
$sheet->getStyleByColumnAndRow($row, $num)
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3);
我可以在我的手机上看到08:00
$date = $start_date->getTimestamp(); $mainSheet = $objPHPExcel->getSheet(0); $mainSheet->setCellValue('B1', PHPExcel_Shared_Date::PHPToExcel($date)); $mainSheet->getStyle('B1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);
但仍然不起作用。 - xmarston41720.618009259
。 - xmarston