如何阅读09/09/2017完全从xls使用php?

var_dump($objPHPExcel->getActiveSheet()->getCell('B2')->getValue()); 

我试过上面的代码,我得到"float(42987)"作为输出。 我只想要那个单元格的确切值!

  var_dump($objPHPExcel->getActiveSheet()->getCell('C2')->getValue()); 

当我执行这个我得到正确的价值"2017/09/12"

那么我如何从xls中获取这个"09/09/2017"格式的数据呢?

在这里输入图像说明

编辑1the 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' 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

如果您正在使用rangeToArray()方法获取数据,请查看rangeToArray()的参数

 /** * Create array from a range of cells * * @param string $pRange Range of cells (ie "A1:B10"), or just one cell (ie "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); 

MS Excel对于date使用序列化的时间戳(有点像一个unix时间戳),这是PHPExcel在调用getValue()时返回的float(42987) getValue() ….请注意,这单元格的确切值。 ..浮动转换为数字格式掩码MS Excel中的date/时间显示。 您在MS Excel中看到的是单元格的格式化值,并应用了数字格式掩码,所以您需要告诉PHPExcel获取格式化的值而不是确切的(原始)值。

只要你没有加载readDataOnly设置为true的文件(告诉PHPExcel 不要加载样式和格式化数据),然后使用

  var_dump($objPHPExcel->getActiveSheet()->getCell('B2')->getFormattedValue()); 

这是一个OADate(OLE自动化date)。 你回来float(42987)是下面的确切的价值。 Excel只是将其显示为您select的任何格式的date。

使用这个类来转换它。

 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('Ymd H:i:s'); return new DateTime("$dateTime.$msecs"); } } 

另外,如果你可以使用JavaScript,使用时刻库​​。 有一个函数来转换OADates TO和FROM。

https://github.com/markitondemand/moment-msdate#about-ole-automation-dates

将OAdate转换为片刻(或JavaScriptdate):

moment.fromOADate(41493)返回Wed Aug 07 2013 00:00:00 GMT-0600 (MDT)

确切的date和时间(时间是小数点右侧的值):

moment.fromOADate(41493.706892280097000)返回Wed Aug 07 2013 16:57:55 GMT-0600 (MDT)

默认情况下, moment.fromOADate()使用服务器时间作为UTC的偏移量,可以提供第二个参数,指示OAdate以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自动化date是未指定的,这意味着它们默认基于本地时区。

我认为有几种方法可以像ExcelToPHP()toFormattedString()

使用后者,您可以将Excel值$value转换为您正在查找的string,如下所示:

 $string = \PHPExcel_Style_NumberFormat::toFormattedString($value, 'DD/MM/YYYY');