Import and export of thinkphp Excel

Source: Internet
Author: User

1  Public functionupload ()2 {3 //Setting the response header4 Header("Contec-type=text.html;charset=utf-8");5 //instantiating an upload class6 $upload=New\think\upload ();7 //Set upload attachment size8$$upload->maxsizae=20480000;9 //Set upload attachment typeTen $upload->exts=Array(' xls ',xlsx); One //set File upload directory A $upload->savpath= "/"; - //Uploading Files - $info=$upload->uploadone ($_files[' File ']); the $filename= '/. Upload '.$info[' Savepath '].$info[' Savename ']; - $exts=$info[' ext ']; - if(!$info) - { + //prompt for upload errors - $this->error ($upload-geterror); +  A } at Else{ - //upload Successful, start import - $this->dataimport ($finame,$exts); -  - } - } in  - //This is the import function toPuhblicfunctionDataImport ($filename,$exts) + { -  the  //Import the Phpexcel class library because Phpexcel does not use a namespace, only inport import *Import ("Org.Util.PHPExcel"); $         //Create a Phpexcel object, note that there is no less \Panax Notoginseng         $PHPExcel=New\phpexcel (); -         //if the Excel file suffix name is. xls, import this class the         if($exts= = ' xls '){ +Import ("Org.Util.PHPExcel.Reader.Excel5"); A             $PHPReader=New\phpexcel_reader_excel5 (); the}Else if($exts= = ' xlsx '){ +Import ("Org.Util.PHPExcel.Reader.Excel2007"); -            $PHPReader=New\phpexcel_reader_excel2007 (); $  $     //Loading Files -         $PHPExcel=$PHPReader->load ($filename); -         //gets the first sheet in the table, if you want to get the second one, change 0 to 1, and so on the         $currentSheet=$PHPExcel->getsheet (0); -         //get total number of columnsWuyi         $allColumn=$currentSheet-Gethighestcolumn (); the         //get total number of rows -         $allRow=$currentSheet-Gethighestrow (); Wu         //loops through the data in the table, $currentRow represents the current row, which row starts reading the data, and the index value starts at 0 -          About          for($currentRow= 3;$currentRow<=$allRow;$currentRow++){ $             //from which column, a represents the first column -              -              for($currentColumn= ' A ';$currentColumn<=$allColumn;$currentColumn++){ -                 //Data coordinates A                 $address=$currentColumn.$currentRow; +                 //the data to be read, saved to the array $arr the                $data[$currentRow][$currentColumn]=$currentSheet->getcell ($address),GetValue (); -                  $             } the         } the         $this->save_import ($data); the  the } -  in //in the Import Guide database the  Public functionSave_import ($data) the { About //assume that a database the $database=d (' Upload '); the //with Loops the foreach($data  as $k=$v) + { - //Encapsulating Data the $info[' Name ']=$v[' B '];Bayi             $info[' Age ']=$v[' C ']; the             $info[' Tel ']=$v[' E ']; the             $info[' Addr ']=$v[' F ']; -             $info[' Career ']=$v[' G ']; -             if($v[' B ']!= "" &&$v[' C ']!= "" &&$v[' E ']!= "" &&$v[' F ']!= "" &&$v[' G ']!= "") the             { the             $result=$d->where (Array(' tel ' = =$v[' E '])) -find (); the              the             if($result){ -               $res=$d->where (Array(' tel ' = =$v[' E '])) ->save ($info); the}Else{ the                 $res=$d->add ($info); the             }94  the  the } the 98  About  -}

Import and export of thinkphp Excel

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