How to import PHPExcel files into Thinkphp3.2.3

零到壹度
Release: 2023-03-23 16:14:02
Original
1933 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:

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 '
'; 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='', $sheet=0){ $file = iconv("utf-8", "gb2312", $file); //转码 if(empty($file) OR !file_exists($file)) { die('file not exists!'); } vendor("PHPExcel.PHPExcel"); // 引入我们自己导入的文件 $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象 if(!$objRead->canRead($file)){ $objRead = new PHPExcel_Reader_Excel5(); if(!$objRead->canRead($file)){ die('No Excel!'); } } $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'); $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
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!