PHPExcelでExcelの時間を読み取るサンプルコードを詳しく紹介

黄舟
リリース: 2023-03-06 19:16:02
オリジナル
3200 人が閲覧しました

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ファイルをネイティブにエクスポートする2つの方法

PHPでExcelドキュメントを取得する コード例コンテンツ用

以上がPHPExcelでExcelの時間を読み取るサンプルコードを詳しく紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!