Home Backend Development PHP Tutorial How to import PHPExcel files into Thinkphp3.2.3

How to import PHPExcel files into Thinkphp3.2.3

Apr 12, 2018 am 10:41 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to import local songs from NetEase Cloud Music How to import local songs How to import local songs from NetEase Cloud Music How to import local songs Mar 13, 2024 am 11:19 AM

When we use this platform to listen to songs, most of them should have some songs that you want to listen to. Of course, some things may not be listened to because there is no copyright. Of course, we can also directly use some songs imported locally. Go up there so you can listen. We can download some songs and directly convert them into mp3 formats, so that they can be scanned on the mobile phone for import and other situations. However, for most users, they don’t know much about importing local song content, so in order to solve these problems well, today the editor will also explain it to you. The content method allows you to make better choices without asking. If you are interested,

How to use Vue to implement Excel import function How to use Vue to implement Excel import function Jul 22, 2023 pm 07:46 PM

How to use Vue to implement Excel import function Vue.js is a popular JavaScript framework for building user interfaces. It provides many convenient features and easy-to-use API, allowing us to quickly build feature-rich applications. In this article, we will introduce how to use Vue.js to implement Excel import function. First, we need to install a library called "xlsx", which is a JavaScript library that parses and extracts Excel file data.

How to import oiv files into openiv How to import oiv files into openiv Feb 21, 2024 pm 11:12 PM

OpenIV is a very convenient tool that can be used to import OIV files in GTA games. OIV files are a specific file format used to package mod or modification files into an easy-to-install format. This article will introduce how to use OpenIV to import OIV files. First, make sure you have the OpenIV software installed. If not, you can download it from the official website and follow the instructions to install it. Next, download the required OIV file from a trusted source. You can find various GTA modding communities or M

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

Complete Guide: How to process Excel files using php extension PHPExcel Complete Guide: How to process Excel files using php extension PHPExcel Jul 28, 2023 pm 10:01 PM

Complete Guide: How to Process Excel Files Using PHP Extension PHPExcel Introduction: Excel files are often used as a common format for data storage and exchange when processing large amounts of data and statistical analysis. Using the PHP extension PHPExcel, we can easily read, write and modify Excel files to effectively process Excel data. This article will introduce how to use the PHP extension PHPExcel to process Excel files and provide code examples. 1. Install PHPExc

PHP development: Use PHPExcel to process Excel files PHP development: Use PHPExcel to process Excel files Jun 15, 2023 pm 03:45 PM

With the advent of the digital age, data has become the most important part of our daily lives and work, and Excel files have become one of the important tools for data processing. I believe that many PHP developers will often encounter the use of Excel files for data processing and operations at work. This article will introduce you to the methods and precautions for using the PHPExcel library to process Excel files. What is PHPExcel? PHPExcel is a PHP class

How to import soda music to an external playlist How to import soda music to an external playlist How to import soda music to an external playlist How to import soda music to an external playlist Mar 22, 2024 pm 03:51 PM

Do you know how to import external playlists on Soda Music App? Many users of the latest version of Soda Music App, when importing some playlists, do not like to import their own songs into the playlist, but this is limited. You can manage the classification of some of your songs very well when you listen to it, so friends who have ideas must give it a try and find out more. I believe you will like it, and you can have any ideas. Step-by-step tutorial for importing external playlists from Soda Music: 1. On the Soda Music app page, click and select the lower right corner to create a playlist. 2. Create a page, enter the playlist name, and click Save. 3. On the My Playlist page, select Add Songs. 4. Search, select your favorite songs, and add them to the playlist

Detailed introduction to the operation method of importing external playlists from QQ Music Detailed introduction to the operation method of importing external playlists from QQ Music Jan 12, 2024 pm 03:09 PM

Everyone has several different music software installed on their mobile phone, and everyone likes to save their favorite songs in their favorites. Today we will talk about QQ Music, which not only supports one-click import of external playlists, but is also very convenient to operate. Next, the editor of this site will lead you to learn in detail how to import external playlists from QQ Music. If you are in need, please follow the editor and take a look! Introducing the detailed operation method of how to import external playlists in QQ Music. First, open QQ Music and enter the homepage. Then, click "My" below to enter my interface. Scroll down and you will see an option to "Import external playlist". Clicking it will jump to another page. On that page, you need to copy the link to the external playlist and

See all articles