Thinkphp implementing the method of importing Excel into the database, thinkphpexcel_php tutorial

Source: Internet
Author: User
Tags php template smarty template

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 ...

  • Related Article

    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.