How to import PHPExcel files into Thinkphp3.2.3

零到壹度
Release: 2023-03-23 16:14:02
Original
1977 people have browsed it

This article will share with you how to import PHPExcel files into Thinkphp3.2.3. It has certain reference value. Friends in need can refer to it

1. First of all, First download the PHPExcel plug-in:

ThinkPHP version: 3.2.3
PHPExcel version: 1.8

##PHPExcel official Download address: https://github.com/PHPOffice/PHPExcel

2. After decompression, it is as follows:


As long as the Classes folder is used, the others are of no use. Change the name of the classes folder to PHPExcel (name it whatever you want)

3. Copy the PHPExcel file to Thinkphp, the location is as follows


4. Everything is ready, open as we journey.


##Front-end code:

<form action="{:U(&#39;Index/upload&#39;)}" method="post" enctype="multipart/form-data">
   <ul>
 
       <li><input type="file" name="files" /></li> 
       <li><input type="submit" value="上传" /></li>
   </ul>
</form>
Copy after login

Backend code(upload method in IndexController.class.php):

public function upload(){    

    if(isset($_FILES["files"]) && ($_FILES["files"]["error"] == 0)){        
    $result = importExecl($_FILES["files"]["tmp_name"]);        
    echo &#39;<pre />&#39;;        
    print_r($result);        
    die;           
      }    
    }
Copy after login


Among them

importExecl

Written in a public method, the location is as follows



##The code of the public method is as follows

:

function importExecl($file=&#39;&#39;, $sheet=0){   
   $file = iconv("utf-8", "gb2312", $file);                  //转码      
   if(empty($file) OR !file_exists($file)) {     
        die(&#39;file not exists!&#39;);      
   }      
   vendor("PHPExcel.PHPExcel");                             // 引入我们自己导入的文件    
   $objRead = new PHPExcel_Reader_Excel2007();   //建立reader对象      
   if(!$objRead->canRead($file)){     
        $objRead = new PHPExcel_Reader_Excel5();          
        if(!$objRead->canRead($file)){        
              die(&#39;No Excel!&#39;);         
        }      
    }        
    
    $cellName = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;,
     &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, 
     &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;, &#39;AA&#39;, &#39;AB&#39;, &#39;AC&#39;, &#39;AD&#39;, &#39;AE&#39;, &#39;AF&#39;, &#39;AG&#39;, &#39;AH&#39;, &#39;AI&#39;,
      &#39;AJ&#39;, &#39;AK&#39;, &#39;AL&#39;, &#39;AM&#39;, &#39;AN&#39;, &#39;AO&#39;, &#39;AP&#39;, &#39;AQ&#39;, &#39;AR&#39;, &#39;AS&#39;, &#39;AT&#39;, &#39;AU&#39;,
       &#39;AV&#39;, &#39;AW&#39;, &#39;AX&#39;, &#39;AY&#39;, &#39;AZ&#39;);    
       
           $obj = $objRead->load($file);  //建立excel对象      
           $currSheet = $obj->getSheet($sheet);   //获取指定的sheet表      
           $columnH = $currSheet->getHighestColumn();   //取得最大的列号      
           $columnCnt = array_search($columnH, $cellName);      
           $rowCnt = $currSheet->getHighestRow();   //获取总行数        
           
           $data = array();      
           for($_row=1; $_row<=$rowCnt; $_row++){  //读取内容        
             for($_column=0; $_column<=$columnCnt; $_column++){         
                  $cellId = $cellName[$_column].$_row;              
                  $cellValue = $currSheet->getCell($cellId)->getValue();               
                  //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue();  #获取公式计算的值              
                  if($cellValue instanceof PHPExcel_RichText){   //富文本转换字符串            
                        $cellValue = $cellValue->__toString();              
                   }                
                   
                   $data[$_row][$cellName[$_column]] = $cellValue;         
                   }      
            }        
            return $data;  
    }
Copy after login
5. Finally upload the XLS file. If successful, it will be printed as follows:



##6. Finally, the code is relatively simple to write. You can verify the type and size yourself, so I won’t write them here. I personally tested the above code and found no problem.

The above is the detailed content of How to import PHPExcel files into Thinkphp3.2.3. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!