日期:2014-05-17  浏览次数:20531 次

phpExcel 正确读取excel表格时间
利用phpExcel读取excel文件,读取出来的时间列出现5位数字,并不是想要的时间(不知道为啥),在网上搜了一个excelTime的函数可以正确转化时间
品牌网站建设:http://www.ucantech.com/

Php代码

    error_reporting(E_ALL); 
    date_default_timezone_set('Asia/shanghai'); 
    /** PHPExcel_IOFactory */ 
    require_once '../Classes/PHPExcel/IOFactory.php'; 
    $inputFileName = '6081076641077444758.xls'; 
    $objReader = new PHPExcel_Reader_Excel5(); 
    $objPHPExcel = $objReader->load($inputFileName); 
    $sheet = $objPHPExcel->getSheet(0); 
    $highestRow = $sheet->getHighestRow(); // 取得总行数 
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数 
    $tempArray = array(); 
    for($j=2;$j<=$highestRow;$j++){ 
       for($k='A';$k<=$highestColumn;$k++){  
        if($k=='M'||$k=='O') //M列和O列是时间 
            $tempArray[] = excelTime($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()); 
        else 
            $tempArray[] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue(); 
        } 
        print_r($tempArray); 
        unset($tempArray); 
    } 
     
    function excelTime($date, $time = false) { 
        if(function_exists('GregorianToJD')){ 
            if (is_numeric( $date )) { 
            $jd = GregorianToJD( 1, 1, 1970 ); 
            $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 ); 
            $date = explode( '/', $gregorian ); 
            $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT ) 
            ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT ) 
            ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT ) 
            . ($time ? " 00:00:00" : ''); 
            return $date_str; 
            } 
        }else{ 
            $date=$date>25568?$date+1:25569; 
            /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/ 
            $ofs=(70 * 365 + 17+2) * 86400; 
            $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : ''); 
        } 
      return $date; 
    }  (fblww-0112)