PHPExcel时间字符串 [英] PHPExcel String to Time

查看:175
本文介绍了PHPExcel时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Excel电子表格.在第二列中,我有类似0:11:232: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:220: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序列化的时间戳,并将该值存储在单元格中;如

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时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆