PHPExcel - Reading Date values

2019-09-17 01:06发布

问题:

I am using PHPExcel library to read spread sheet data. This library is giving me trouble reading the 'DATE' values.

Question: How to instruct the PHPExcel to read the 'DATE' values properly even if 'setReadDataOnly' is set as 'false'?

Description:

PHPExcel version: 2.1, Environment: Ubuntu 12

Here is the code block:

$objReader = \PhpExcel\PHPExcel_IOFactory::createReaderForFile($spreadSheetFullFilePathString);
$objReader->setReadDataOnly(true); 
$objPHPExcel = $objReader->load($spreadSheetFullFilePathString);
$objWorksheetObject = $objPHPExcel->getActiveSheet();

Am getting 'integer' values for the 'DATE' column values by default.

I found that, the line '$objReader->setReadDataOnly(true)' is causing the trouble. So, Changed it as '$objReader->setReadDataOnly(false)'.

I am getting the Date column value properly after this change. But now the reader is reading ALL the ROWS + COLUMNS found in the excel.

Any help would be greatly appreciated.

回答1:

If $objReader->setReadDataOnly(true);, then the only reader that can identify dates is the Gunumeric Reader. A date value can only be identified by the number format mask, and setReadDataOnly(true) tells the Reader not to read the formatting, so dates cannot subsequently be identified (Gnumeric is the exception, because it was written after the problem was identified, but none of the other Readers have yet been modified to read this information regardless of the setReadDataOnly value. If you need to identify dates, then the only option at this point is $objReader->setReadDataOnly(false);

However, for date cells, you shouldn't get an integer value; you should get a float: it's the decimal that identifies the time part of the Excel datetime serialized value.

If you know which cells contain dates, then you can convert them to unix timestamps or PHPExcel DateTime objects using the helper functions defined in the PHPExcel_Shared_DateTime class (and can then use all the standard PHP date handling functions).

You say that the reader is now reading ALL the ROWS + COLUMNS found in the excel: it should, irrespective of the setReadDataOnly value. If you don't want to read all the rows and columns, then you need to set a Read Filter.



标签: phpexcel