I'm using PHPExcel library, I extract the content of a column cell, that cell has a date, in excel it is displayed as "30/07/2014" but when I extract it using the library it dispalyes : 41850 as date value, I can't rely on the excel formatting cells because each time I get a different file with date so my question is , is there a way to convert the value 41850 using the library to the original date text 30/07/2014?
The code I'm using is :
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$outPut = $objPHPExcel->getSheet(0)->getCellByColumnAndRow(6, 6)->getFormattedValue();
echo $outPut;//41850
Your problem is
$objReader->setReadDataOnly(true);
You're telling PHPExcel only to read the raw data from the cell, without any formatting information; but MS Excel uses a float for dates (number of days since 1/1/1900 or since 1/1/1904, depending on the calendar used) and a number format mask to format it as a date.... by setting read data only to true, you're telling PHPExcel not to read the number format masks, so there is no way of differentiating a date value from any other float value.
Solution: don't use
$objReader->setReadDataOnly(true);
Related
I would like to be able to filter my data like this:
Year ( + )
Month ( + )
Day ( + ).
At first i tought it's problem of cell formatting cause the format was set to General. So i did this:
$objPHPExcel->getActiveSheet()
->getStyle('G2:G256')
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2
);
And i worked. Now my cell G2 is formatted as date (at least excel says so). But i'm not able to sort this until i select cell and hit enter, after this ONLY this one cell is sortable. I have no idea why.
Thanks in advance for any help!
You need to set date values in cells as MS Excel timestamp values, not strings or unix timestamps.
Assuming from the way you're doing it at the moment that $row->user_created is a Unix timestamp value the, rather than
$formattedRow[] = date('Y-m-d',$row->user_created);
use
$formattedRow[] = PHPExcel_Shared_Date::PHPToExcel(
$row->user_created
);
which will set the value to a MS Excel serialized date/timestamp
You still want to set the number format mask, so that MS Excel knows how the date should be displayed; but as it's stored in Excel format rather than as a string, it will be sortable or filterable, or usable in formulae, etc
I should read data from more than 4 different excel file with different cell formating but same data within, so how i can change the cell format then read the data using phpexcel?
If you're storing a numeric value that's longer than a 32-bit signed integer can handle (such as 435546567567345) then treat it as a string using
$objPHPExcel->getActiveSheet()
->setCellValueExplicit(
'A1',
'435546567567345',
PHPExcel_Cell_DataType::TYPE_STRING
)
EDIT
If you're reading this value from an Excel worksheet, and it is actually a number value rather than a string containing a numeric value, then it is likely being treated as a float by MS Excel, so there may well be some loss of precision already (unless the file was created using a 64-bit version of MS Excel), even before PHPExcel reads it. If it is a number created using a 64-bit version of MS Excel, then you'll need a 64-bit version of PHP to read it without loss of precision.
Try reading the raw, unformatted value using getValue() and then doing a var_dump() to see what datatype it actually is; or try using getDataType() to see what the value was being stored as in the Excel file
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.
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.
Good Morning,
A program in SAS is about to select/merge/sort dates/times in alphanumeric value (ex : 14-Jan-2013 07:00:00.479) inside a lot of tables and to create a single table.
This program use the instruction "format E8601" and several lines after "format type $10.;
informat type $10.;", what transforms the dates in a numeric value (ex : 2013-01-14T07:02:03.647).
When this table is exported in Excel 2007, the value becomes " " and can't be modified in a traditional date/time format.
How to do it ? Is there any other format (instead of E8601) which can be used to keep the date in text or in a alphanumeric value ?
Thanks for your help.
SAS datetime values are internally represented as floating point values equal to the number of seconds since January 1, 1960. FORMATS are used to control how those numeric values are externally represented. For example, consider this:
data have;
myDateTime1 = '14-Jan-2013 07:00:00.479'dt;
myDateTime2 = '14-Jan-2013 07:00:00.479'dt;
myDateTime3 = '14-Jan-2013 07:00:00.479'dt;
format myDateTime2 datetime23.3
myDateTime3 E8601DT23.3;
put myDateTime1= 'as a number'
/ myDateTime2= 'as a normal SAS datetime'
/ myDateTime3= 'as an ISO 8601 datetime'
;
run;
When run, this is shown in the SAS log:
myDateTime1=1673766000.5 as a number
myDateTime2=14JAN2013:07:00:00.479 as a normal SAS datetime
myDateTime3=2013-01-14T07:00:00.479 as an ISO 8601 datetime
Note the three myDateTime variables have the same value but are displayed differently based on the format specified.
Assuming you have SAS Access to PC File Formats licensed, you can just use PROC EXPORT to create an Excel workbook:
proc export data=have
outfile='c:\temp\test_dates.xlsx'
replace;
run;
The data values in the Excel workbook for the two variables formatted as "datetime" values will appear correctly as Excel columns. However, the default formatting in Excel only shows the "date" portion; to display the complete value in Excel you will need to change the Excel column formats.
I've am trying to format cells in an Excel document I create with PHPExcel using the setFormatCode method. I've been going through the list of 'FORMAT_DATE_xxxxxxx' options associated with this method, but I've been unable to find one that lists the month, date, and year in that order. Did I miss something here?
You missed FORMAT_DATE_XLSX14 and FORMAT_DATE_XLSX22
You also missed the fact that these are simply predefined string values, and that you can use any valid Excel numberformat string value that you set yourself.
$objPHPExcel->getActiveSheet()->getStyle('C9')
->getNumberFormat()
->setFormatCode(
'mm-dd-yyyy' // my own personal preferred format that isn't predefined
);