ThinkPHP implements the method of importing excel into the database, thinkphpexcel. ThinkPHP implements how to import excel to the database. thinkphpexcel in this article describes how thinkPHP Imports excel to the database. For your reference, thinkPHP introduces how to import excel to 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.
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 ");}}