请教各位,我在使用phpexcel读取excel文件,当excel单元格的类型为“日期”时,phpexcel读出来的是一个类似21311的数,这个问题怎么解决?
另外phpexcel怎么判断excel单元格的数据是整形、字符型还是日期?谢谢大家!
<?
require_once 'PHPExcel-1.7.3/PHPExcel-1.7.3/PHPExcel.php';//包含类   
require_once 'PHPExcel-1.7.3/PHPExcel-1.7.3/PHPExcel/IOFactory.php';  $objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("excel.xls");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); //
$highestColumn = $objWorksheet->getHighestColumn(); //
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // echo '<table>' . "\n";
//for ($row = 2; $row <= $highestRow; ++$row) {
  echo '<tr>' . "\n";
  for ($col = 0; $col <= $highestColumnIndex; ++$col) {   
    echo '<td>[' . $objWorksheet->getCellByColumnAndRow($col, 3)->getValue() . ']</td>' . "\n";
if($col==7)//得到日期结果
  {
      echo $objWorksheet->getCellByColumnAndRow($col, 3)->getValue();
 }
  }
  echo '</tr>' . "\n";
//}
echo '</table>' . "\n";
?>

解决方案 »

  1.   

    日期是2008-2-29  但是输出的结果是39507  
    date('yyyy-mm-dd', $date); 这样转换的话是错误的
      

  2.   

    解决方法:
    //excel日期转换函数
    function excelTime($days, $time=false){
    if(is_numeric($days)){
    //based on 1900-1-1
    $jd = GregorianToJD(1, 1, 1970);
    $gregorian = JDToGregorian($jd+intval($days)-25569);
    $myDate = explode('/',$gregorian);
    $myDateStr = str_pad($myDate[2],4,'0', STR_PAD_LEFT)
    ."-".str_pad($myDate[0],2,'0', STR_PAD_LEFT)
    ."-".str_pad($myDate[1],2,'0', STR_PAD_LEFT)
    .($time?" 00:00:00":'');
    return $myDateStr;
    }
    return $days;
    }