php - PHPExcel toArray is changing date and time columns from XLSX format -
i using exceltoarray function found here: https://gist.github.com/calvinchoy/5821235
function exceltoarray($filepath, $header = true) { require_once("./phpexcel/classes/phpexcel.php")); //create excel reader after determining file type $inputfilename = $filepath; /** identify type of $inputfilename **/ $inputfiletype = phpexcel_iofactory::identify($inputfilename); /** create new reader of type has been identified **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** set read type read cell data onl **/ $objreader->setreaddataonly(true); /** load $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename); //get worksheet , built array first row header $objworksheet = $objphpexcel->getactivesheet(); //excel first row header, use header key if($header){ $highestrow = $objworksheet->gethighestrow(); $highestcolumn = $objworksheet->gethighestcolumn(); $headingsarray = $objworksheet->rangetoarray('a1:'.$highestcolumn.'1',null, true, true, true); $headingsarray = $headingsarray[1]; $r = -1; $nameddataarray = array(); ($row = 2; $row <= $highestrow; ++$row) { $datarow = $objworksheet->rangetoarray('a'.$row.':'.$highestcolumn.$row,null, true, true, true); if ((isset($datarow[$row]['a'])) && ($datarow[$row]['a'] > '')) { ++$r; foreach($headingsarray $columnkey => $columnheading) { $nameddataarray[$r][$columnheading] = $datarow[$row][$columnkey]; } } } } else{ //excel sheet no header $nameddataarray = $objworksheet->toarray(null,true,true,true); } return $nameddataarray; }
i have 2 versions of spreadsheet, 1 in csv , 1 in excel. here are:
csv: https://drive.google.com/open?id=0b2gilrtnrtzkd3v3aevet1nqsw8
xlsx: https://drive.google.com/open?id=0b2gilrtnrtzkdzjnznh0cmhpa1e
when upload csv using function , var_dump
results
array (size=58) 0 => array (size=4) 'pd' => string '11/10/2016' (length=10) 'pt' => string '9:12' (length=4) 'fd' => string '11/10/2017' (length=10) 'ft' => string '9:12' (length=4) 1 => array (size=4)...
but when upload xlsx get:
array (size=58) 0 => array (size=5) 'pd' => float 42684 'pt' => float 0.38333333333333 'fd' => float 43049 'ft' => float 0.38333333333333 '' => null 1 => array (size=5)
notice pd goes 11/10/2016 42684, , pt 9:12 0.38333...
what causing xlsx file not read in displays?
i have read other stack questions, i appear passing toarray correct values. not sure i' missing...
ms excel stores dates serialized timestamp, number of days since 1t january 1900 (or 1st january 1904, depending on whether using windows or mac calendar). phpexcel likewise, dates/times loaded store in spreadsheet object in way ms excel works them.
so when load file human format date, reads ms excel serialized timestamp. ordinarily, store number format mask telling phpexcel cell contains timestamp value shoud formatted date, you're telling phpexcel's loader not take additional action using $objreader->setreaddataonly(true);
means store data, , not formatting information. because phpexcel desn't hve additional formatting information, cannot know cell contains should dispayed date, can dispay serialized tiemstamp, float.
in other words, don't $objreader->setreaddataonly(true);
if want dates treated dates, or unless want date handling yourself
Comments
Post a Comment