var_dump($objPHPExcel->getActiveSheet()->getCell('B2')->getValue());
我尝试了上面的代码,我得到"float(42987)"
作为输出。
我只想要那个细胞的确切值!
var_dump($objPHPExcel->getActiveSheet()->getCell('C2')->getValue());
当我执行此操作时,我得到正确的值"2017/09/12"
那么如何从xls获取此"09/09/2017"
格式的数据?
编辑1 :
the data cannot be predicted ! it can be a string or date with different formats !
example : B2 can be 'string','09/09/2017','09-09-2017','2017/09/09','10/20-25/14'
就像这个例子一样,任何数据都可以存在!所以我只想要用户提供的单元格中的确切数据!
编辑2 :我正在使用rangeToArray
for ($row = 2; $row <= $highestRow; $row++){
$rowData = $sheet->rangeToArray('A'.$row.':' . $highestColumn.$row,NULL,TRUE,FALSE);
}
那么如何在->getFormattedValue()
中实施rangeToArray
?
答案 0 :(得分:2)
MS Excel对日期使用序列化时间戳(有点像unix时间戳),这是PHPExcel在您调用float(42987)
时返回的getValue()
....请注意 单元格的确切值....该浮点数在MS Excel中通过数字格式掩码转换为日期/时间显示。您在MS Excel中看到的是单元格的格式化值,并且应用了数字格式掩码,因此您需要告诉PHPExcel获取格式化值而不是精确(原始)值。
只要你没有加载readDataOnly设置为true的文件(告诉PHPExcel 不 加载样式和格式化数据),那么使用
var_dump($objPHPExcel->getActiveSheet()->getCell('B2')->getFormattedValue());
答案 1 :(得分:2)
如果您使用rangeToArray()
方法获取数据,请查看rangeToArray()
/**
* Create array from a range of cells
*
* @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
* @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
* @param boolean $calculateFormulas Should formulas be calculated?
* @param boolean $formatData Should formatting be applied to cell values?
* @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
* True - Return rows and columns indexed by their actual row and column IDs
* @return array
*/
因此,要获取rangeToArray()
返回的格式化值,您需要将$formatData
参数设置为true
$rowData = $sheet->rangeToArray('A'.$row.':' . $highestColumn.$row,NULL,TRUE,TRUE);
答案 2 :(得分:1)
我认为有几种方法可以做到这一点,例如ExcelToPHP()
和toFormattedString()
。
使用后者,您可以将Excel值$value
转换为您要查找的字符串,如:
$string = \PHPExcel_Style_NumberFormat::toFormattedString($value, 'DD/MM/YYYY');
答案 3 :(得分:1)
这是一个OADate(OLE自动化日期)。你得到的回报float(42987)
是下面的确切值。 Excel只会以您选择的任何格式将其显示为日期。
使用此类进行转换。
class OLEAutomationDateConverter
{
/**
* Get the OLE Automation Date epoch
*
* @return DateTimeImmutable
*/
public static function BaseDate()
{
static $baseDate = null;
if ($baseDate == null) {
$baseDate = new DateTimeImmutable('1899-12-30 00:00:00');
}
return $baseDate;
}
/**
* Convert a DateTime object to a float representing an OLE Automation Date
*
* @param DateTimeInterface $dateTime
* @return float
*/
public static function DateTimeToOADate(DateTimeInterface $dateTime)
{
$interval = self::BaseDate()->diff($dateTime);
$mSecs = ($interval->h * 3600000)
+ ($interval->i * 60000)
+ ($interval->s * 1000)
+ floor($dateTime->format('u') / 1000);
return $interval->days + ($mSecs / 86400000);
}
/**
* Convert a float representing an OLE Automation Date to a DateTime object
*
* The returned value has a microsecond component, but resolution is millisecond and even
* this should not be relied upon as it is subject to floating point precision errors
*
* @param float $oaDate
* @return DateTime
*/
public static function OADateToDateTime($oaDate)
{
$days = floor($oaDate);
$msecsFloat = ($oaDate - $days) * 86400000;
$msecs = floor($msecsFloat);
$hours = floor($msecs / 3600000);
$msecs %= 3600000;
$mins = floor($msecs / 60000);
$msecs %= 60000;
$secs = floor($msecs / 1000);
$msecs %= 1000;
$dateTime = self::BaseDate()
->add(new DateInterval(sprintf('P%sDT%sH%sM%sS', $days, $hours, $mins, $secs)))
->format('Y-m-d H:i:s');
return new DateTime("$dateTime.$msecs");
}
}
或者,如果您可以使用javascript,请使用时刻库。有一个函数可以转换OADates TO和FROM。
https://github.com/markitondemand/moment-msdate#about-ole-automation-dates
将OA日期转换为片刻(或转换为JavaScript日期):
moment.fromOADate(41493)
返回Wed Aug 07 2013 00:00:00 GMT-0600 (MDT)
对于确切的日期和时间(时间是小数点右边的值):
moment.fromOADate(41493.706892280097000)
返回Wed Aug 07 2013 16:57:55 GMT-0600 (MDT)
默认情况下,moment.fromOADate()
使用服务器时间作为UTC的偏移量,可以提供第二个参数,表示OA日期与UTC的偏移量,以分钟为单位。
moment.fromOADate(42754.835023148145, 360)
返回Fri Jan 20 2017 02:02:25 GMT+0000 (UTC)
对于时刻格式:
//convert OA date into Moment (JavaScript date)
var momentDate = moment.fromOADate(41493.706892280097000);
//use Moment's awesomeness
var formattedDate = momentDate.format('MMM Do YY);
//formattedDate === "Aug 7th 13"
这很容易被链接在一起:
moment.fromOADate(41493.706892280097000).format('MMM Do YY); //Aug 7th 13
注意:未指定OLE自动化日期,这意味着默认情况下它们基于本地时区。