Thinkphp implements the method of importing Excel into the database, Thinkphpexcel
This article describes a method that thinkphp implements to import Excel into a database. Share to everyone for your reference, as follows:
Phpexcel plug-in click here to download the site.
Here is the thinkphp framework of the 3.1 version, download the compressed package, the framework of the extend in the Vendor folder to create a 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
The following is the PHP
function Abcdefgwulisuibianuplod () {$this->display ();//Display page}function Abcdefgwulisuibian () {if (!empty ($_files)) { Import ("@.org.uploadfile"); $config =array (' allowexts ' =>array (' xlsx ', ' xls '), ' savepath ' = '. '/public/upload/', ' saverule ' and ' 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 (); Total number of rows obtained $highestColumn = $sheet->gethighestcolumn (); Total number of columns for ($i =2; $i <= $highestRow; $i + +)//This place is usually the first line of the name as needed, so start the loop from the second line, or start with the first line {$data [' lianjieid '] = $objPH Pexcel->getactivesheet ()->getcell ("A". $i)->getvalue ();//DataThe library field and Excel column should correspond $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 succeeded! '); }else {$this->error ("Please select the uploaded file"); }}
More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.
http://www.bkjia.com/PHPjc/1122889.html www.bkjia.com true http://www.bkjia.com/PHPjc/1122889.html techarticle thinkphp implements the method of importing Excel into the database, Thinkphpexcel This article describes the Thinkphp implementation method that imports Excel into the database. Share to everyone for your reference, specific ...