First download the Phpexcel plugin
Http://pan.baidu.com/s/1hq56dFm
I use the thinkphp Framework version 3.1, download the compressed package, the framework of the extend in the vendor folder in the new folder named Phpexcel, put the contents of the classes inside
Here is the front page
Hint: I encountered an error while testing Exception ' phpexcel_reader_exception ' with message ' the filename '
The reason is because the file suffix of Excel may be different, my file suffix is xlsx, and then give the file that saves him as XLS, it can be
<HTML> <Head> </Head> <Body> <formAction= "{pigcms::u (' Jdb/abcdefgwulisuibian ')}"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "Import"/> <inputtype= "hidden"name= "Table"value= "TableName"/> <inputtype= "Submit"value= "Import"/> </form> </Body> </HTML>
The following is the PHP
functionAbcdefgwulisuibianuplod () {$this-display ();//Show Page}functionAbcdefgwulisuibian () {if(!Empty($_files) {import ("@.org.uploadfile"); $config=Array( ' Allowexts ' =Array(' xlsx ', ' xls '), ' savepath ' = './public/upload/', ' saverule ' and ' time ', ); $upload=NewUploadFile ($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 ();//total number of rows obtained $highestColumn=$sheet->gethighestcolumn ();//total number of columns obtained for($i= 2;$i<=$highestRow;$i++)//This place is usually the first row is the name, so start the loop from the second line, or start with the first line {$data[' lianjieid '] =$objPHPExcel->getactivesheet ()->getcell ("A".$i),getValue ();//database fields correspond to Excel columns$data[' yaoqingma '] =$objPHPExcel->getactivesheet ()->getcell ("B").$i),GetValue (); $data[' Dlmima ']=$objPHPExcel->getactivesheet ()->getcell ("C").$i),GetValue (); $data[' Ljdizhi ']=$objPHPExcel->getactivesheet ()->getcell ("D").$i),GetValue (); M (' Jdb ')->add ($data);//Insert Database}$this->success (' Import successful! ‘); }Else { $this->error ("Please select files to upload"); } }
Thinkphp importing Excel into the database