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 ");}}