PHPexcel 判断日期类型
若已经确定某列为日期型数据:
1 for($currentRow=2;$currentRow <= $allRow;$currentRow++){ 2 //从哪列开始,A表示第一列 3 for($currentColumn='A';$currentColumn <= $allColumn;$currentColumn++){ 4 //数据坐标 5 $address=$currentColumn.$currentRow; 6 if($currentColumn =='E'){ 7 $cvalue = gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($PHPExcel->getActiveSheet()->getCell($address)->getValue())); 8 9 } 10 else{ 11 $cvalue = $currentSheet->getCell($address)->getValue(); 12 } 13 14 $arr[$currentRow][$currentColumn]=$cvalue; 15 } 16 17 }
若先判断,再设置
1 for($currentRow=1;$currentRow<=$allRow;$currentRow++){ 2 //从哪列开始,A表示第一列 3 for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){ 4 //数据坐标 5 $address=$currentColumn.$currentRow; 6 $cell = $currentSheet->getCell($address); 7 $cvalue = $cell->getValue(); 8 9 if($cell->getDataType()==\PHPExcel_Cell_DataType::TYPE_NUMERIC){ 10 $cellstyleformat = $cell->getStyle($cell->getCoordinate()) 11 ->getNumberFormat(); 12 13 $formatcode = $cellstyleformat->getFormatCode(); 14 15 if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatcode)) { 16 $cvalue = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP($cvalue)); 17 } 18 else{ 19 $cvalue=\PHPExcel_Style_NumberFormat::toFormattedString($cvalue,$formatcode); 20 } 21 } 22 23 //读取到的数据,保存到数组$arr中\ 24 $arr[$currentRow][$currentColumn]=$cvalue; 25 } 26 }