PHPExcel如何在单元格中设置日期 [英] PHPExcel How to set a date in cell
问题描述
我要把一个日期放在一个单元格中,当我看看它的格式时,看起来像* 14/03/01。
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 calculated values ignores the date I entered because it compares in a calendar (well, a column with all the dates of the actual year) the date that I entered with the dates to set a proper value corresponding the date entered.
有没有办法放置Excel预期的格式?
Is there a way to put the format that Excel expects?
推荐答案
MS Excel为日期使用时间戳值,然后将其屏蔽以进行显示;不是格式化的字符串。
MS Excel uses a timestamp value for dates, and then masks it for display purposes; not a formatted string.
从 02types.php
/ Examples
文件夹:
$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指示单元格包含应显示为日期和/或时间的值
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
这篇关于PHPExcel如何在单元格中设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!