Detailed introduction to the sample code for PHPExcel to read Excel time

黄舟
Release: 2023-03-06 19:16:02
Original
3254 people have browsed it

PHPExcel reads Excel time

    //读取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");                    
        }                
        }
Copy after login

Related articles:

How to handle scientific notation when exporting excel

Two methods of natively exporting excel files in php

Code examples for obtaining the content of excel documents through php

The above is the detailed content of Detailed introduction to the sample code for PHPExcel to read Excel time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template