问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/25580716/get-calculated-date-cell-in-phpexcel