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         }

 

转载于:https://www.cnblogs.com/gaojianli/p/5262316.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值