詳細介紹PHPExcel讀取Excel時間的範例程式碼

黄舟
發布: 2023-03-06 19:16:02
原創
3199 人瀏覽過

PHPExcel讀取Excel 時間

    //读取Excel文件            
    $info = pathinfo ( $path );            
    $ext = $info ['extension'];            
    $excelReader = null;            
    if ( $ext == 'xls' )                
    $excelReader = new  PHPExcel_Reader_Excel5();            
    else                
    $excelReader = new  PHPExcel_Reader_Excel2007();            
    $phpExcelInstance = $excelReader->load($path);            
    $sheet = $phpExcelInstance -> getSheet ( 0 );            
    // 读取第一個工作表            
    $highestRow = $sheet->getHighestRow(); // 取得总行数            
    $highestColumm = $sheet->getHighestColumn(); // 取得总列数            
    /** 循环读取每个单元格的数据 **/            
    $arr = array ();            
    for ( $row = 2 ; $row <= $highestRow ; $row ++ )            
    { // 行数是以第2行开始                
    $cell = $sheet->getCellByColumnAndRow(0, $row);                
    $value = $cell->getValue();                
    if (empty($create_date) || !isset($create_date))                
    {                    
    $create_date = date("Y-m-d");                
    }                
    else                
    {                    
    if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)                    
    {                        
    $cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型                        
    $formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy                      
    if (preg_match(&#39;/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i&#39;, $formatcode))                        
    {                            
    $create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));                        
    }else{                            
    $create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);                       
     }                    
     }                    
     else                    
     {                        
     $create_date = date("Y-m-d");                    
     }                
     }    
     //读取Excel文件            
     $info = pathinfo ( $path );            
     $ext = $info [&#39;extension&#39;];            
     $excelReader = null;            
     if ( $ext == &#39;xls&#39; )                
     $excelReader = new  PHPExcel_Reader_Excel5();            
     else                
     $excelReader = new  PHPExcel_Reader_Excel2007();           
      $phpExcelInstance = $excelReader->load($path);            
      $sheet = $phpExcelInstance -> getSheet ( 0 );            
      // 读取第一個工作表            
      $highestRow = $sheet->getHighestRow(); // 取得总行数            
      $highestColumm = $sheet->getHighestColumn(); // 取得总列数            
      /** 循环读取每个单元格的数据 **/            
      $arr = array ();            
      for ( $row = 2 ; $row <= $highestRow ; $row ++ )            
      { // 行数是以第2行开始                
      $cell = $sheet->getCellByColumnAndRow(0, $row);                
      $value = $cell->getValue();                
      if (empty($create_date) || !isset($create_date))                
      {                   
       $create_date = date("Y-m-d");                
       }                
       else                
       {                    
       if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)                    
       {                        
       $cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型                        
       $formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy                      
       if (preg_match(&#39;/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i&#39;, $formatcode))                        
       {                            
       $create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));                        
       }else{                            
       $create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);                        
       }                   
        }                    
        else                    
        {                        
        $create_date = date("Y-m-d");                    
        }                
        }
登入後複製

相關文章:

php匯出excel時科學計數法的處理方法

#php原生匯出excel檔案的兩種方法

透過php實作取得文件內容的程式碼實例

#

以上是詳細介紹PHPExcel讀取Excel時間的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!