ThinkPHP implements the method of importing excel into the database, thinkphpexcel

Source: Internet
Author: User

ThinkPHP implements the method of importing excel into the database, thinkphpexcel

This example describes how to import an excel file to a database using thinkPHP. We will share this with you for your reference. The details are as follows:

Click here to download the PHP Excel plug-in.

Here we use version 3.1 of the thinkphp framework. Download the compressed package. In the extend of the Framework, create a folder named PHPExcel in the vendor folder, and put the content in the classes into it.

Below is the front-end page

Tip: I encountered The exception 'phpexcel _ Reader_Exception 'with message' The filename during The test.

The reason is that the file suffixes in excel may be different. My file suffix is xlsx, And then I saved it as an xls file.

<Html> 

Below is the php

Function abcdefgwulisuibianuplevels () {$ this-> display (); // display page} function abcdefgwulisuibian () {if (! Empty ($ _ FILES) {import ("@. ORG. uploadFile "); $ config = array ('allowexts '=> array ('xlsx', 'xls'), 'savepath' => '. /Public/upload/', 'saverule' => 'time',); $ upload = new UploadFile ($ config); if (! $ Upload-> upload () {$ this-> error ($ upload-> getErrorMsg ();} else {$ info = $ upload-> getUploadFileInfo ();} vendor ("PHPExcel. PHPExcel "); $ file_name = $ info [0] ['savepath']. $ info [0] ['savename']; $ objReader = PHPExcel_IOFactory: createReader ('excel5'); $ objPHPExcel = $ objReader-> load ($ file_name, $ encode = 'utf-8'); $ sheet = $ objPHPExcel-> getSheet (0); $ highestRow = $ sheet-> getHighestRow (); // get the total number of rows $ highest Column = $ sheet-> getHighestColumn (); // obtain the total number of columns for ($ I = 2; $ I <= $ highestRow; $ I ++) // as needed, the first line is generally the name, so the loop starts from the second line, you can also start from the first line {$ data ['lianjieid'] = $ objPHPExcel-> getActiveSheet ()-> getCell ("". $ I)-> getValue (); // The database Field corresponds to the excel column $ data ['aoqingm'] = $ objPHPExcel-> getActiveSheet () -> getCell ("B ". $ I)-> getValue (); $ data ['dlmim'] = $ objPHPExcel-> getActiveSheet ()-> getCell ("C ". $ I)-> getValue (); $ data ['ljdizhi'] = $ obj PHPExcel-> getActiveSheet ()-> getCell ("D ". $ I)-> getValue (); M ('jdb')-> add ($ data); // insert database} $ this-> success ('import successful! ');} Else {$ this-> error ("select the uploaded file ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.