问题描述
我已经创建了一个Excel电子表格.在第二列中,我有类似0:11:23
和2:03:33
的值,价值几千行.使用PHP,我将格式设置为:
I have created an Excel spreadsheet. In the second column I have values like 0:11:23
and 2:03:33
, several thousand rows worth. Using PHP I set the format to be:
$sheet->getStyle($colRange)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
但是单元格仍然只是字符串.如果您将单元格从0:11:22
编辑为0:11:33之类的其他内容,则该单元格将转换为时间.在顶部,从0:11:22
到0:11:33 AM
的这种自动添加似乎是巨大的变化,然后我可以对文件进行所需的操作.
But the cells are all still just strings. If you edit a cell from 0:11:22
to something else like 0:11:33 then the cell WILL get converted into a time. At the top it goes from 0:11:22
to 0:11:33 AM
this auto addition of AM is what seems to be the huge change, and then I can do what I would like with the file.
如何将数千个单元格从0:11:22
更改为0:11:22 AM
?
How can I change the thousands of cells from 0:11:22
to 0:11:22 AM
?
推荐答案
这是因为您必须将日期/时间转换为MS Excel序列化的时间戳,并将该值存储在单元格中;如 PHPExcel文档,并显示在 /Examples
文件夹中的02types.php .
That's because you have to convert the date/time to an MS Excel serialized timestamp, and store that value in the cell; as described in the PHPExcel Documentation and shown in 02types.php in the /Examples
folder.
一旦存储了实际的MS Excel时间戳记值,就可以对其应用格式
Once you've stored an actual MS Excel timestamp value, then you can apply a format to it
$objPHPExcel->getActiveSheet()->setCellValue('A12', 'Date/Time')
->setCellValue('B12', 'Time')
->setCellValue('C12', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objPHPExcel->getActiveSheet()
->getStyle('C12')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
这篇关于PHPExcel时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!