Thinkphp importing Excel into the database

Source: Internet
Author: User

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

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.