在PHPExcel中获取计算的日期单元格 [英] get calculated date cell in PHPExcel

查看:213
本文介绍了在PHPExcel中获取计算的日期单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PHPExcle导入了一个excel文件。我发现无法从计算得出的单元格中获取正确的值。
计算出的值是该单元格中的 8月27日(中文日期),表示 8月27日英文。
在Sheet2中单元格引用A1,在Sheet2中A1引用另一个Excel文件中的单元格。



我已经尝试了以下方法:

  echo $ cell-> getValue(); // = Sheet2!A2 
echo $ cell-> __ toString(); // = Sheet2!A2
echo $ cell-> getCalculatedValue(); // #REF!
echo $ cell-> getFormattedValue(); // #REF!
echo $ cell-> getOldCalculatedValue(); // 41878(奇怪的结果,我不知道这是什么意思)

我如何获得PHPExcel中的正确值( 8月27日)?

解决方案

您从对 getOldCalculatedValue()的调用中获得的 41878 结果是MS Excel序列化的日期/时间戳。 / p>

在MS Excel中,日期或时间值以自1900年1月1日(如果使用Mac 1904日历,则为1904年1月1日)起的天数来衡量,并隐含为一个号码。只是将其呈现为日期和/或时间的单元格的数字格式掩码



41878



PHPExcel具有内置功能,可将MS Excel序列化的日期/时间戳转换为unix时间戳或一个PHP DateTime对象

  $ unixTimeStamp = PHPExcel_Shared_Date :: ExcelToPHP($ cell-> getOldCalculatedValue()); 

然后使用普通的PHP日期函数对其进行格式化



或者您可以使用

  $ dateTimeObject = PHPExcel_Shared_Date :: ExcelToPHPObject($ cell-> getOldCalculatedValue()) ; 

并使用常规的PHP DateTime对象方法对其进行格式化


I used PHPExcle to import an excel file. I found I could not get correct value from a calculated cell. The calculated value is 8月27日(a date in Chinese) in that cell, it means 27 August in English. The cell references to A1 in Sheet2, and A1 in Sheet2 references to a cell in another excel file.

I've already tried some ways as below:

  echo $cell->getValue();               // =Sheet2!A2
  echo $cell->__toString();             // =Sheet2!A2
  echo $cell->getCalculatedValue();     // #REF!
  echo $cell->getFormattedValue();      // #REF!
  echo $cell->getOldCalculatedValue();  // 41878 (a strange result, I don't know what it means)

How can I get the correct value(8月27日) in PHPExcel?

解决方案

The 41878 result that you get from the call to getOldCalculatedValue(); is an MS Excel serialized date/timestamp.

In MS Excel, a date or time value is measured as the number of days since 1st January 1900 (or 1st January 1904 if using the Mac 1904 calendar) and imply stored as a number. It's only the number format mask for the cell that renders it as a date and/or time

41878 is the number that corresponds to 27th August 2014 in the normal Windows 1900 Calendar.

PHPExcel has functions built-in to convert that MS Excel serialized date/timestamp to either a unix timestamp or a PHP DateTime object

$unixTimeStamp = PHPExcel_Shared_Date::ExcelToPHP($cell->getOldCalculatedValue());

and then use the normal PHP date functions to format it

or you can use

$dateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject($cell->getOldCalculatedValue());

and use normal PHP DateTime object methods to format it

这篇关于在PHPExcel中获取计算的日期单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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