Thinkphp Importing Excel Files (using Phpexcel)

Source: Internet
Author: User
Tags rowcount

The main point of knowledge, using Phpexcel to import Excel data after these days of testing or can, XLS,XLSX can get Excel data.
: http://phpexcel.codeplex.com/

O, development ideas

1. Upload the Excel file to the server first

2. Get the contents of the server Excel file

3. Writing to the database

First, upload Excel file, using PHP's own Upload Method "\think\upload ();", can be easily implemented. For this, I'll tidy up the simplest way to use this method.

/** * TODO Upload file method * @param the name value of $fileid form form file * @param $dir uploaded to the $dir folder in the uploads directory * @param int $maxsize maximum upload limit, default 1 024000 byte * @param array $exts allow upload of file type default array (' GIF ', ' jpg ', ' jpeg ', ' BMP ', ' png ') * @return Array returns array, failed status=0 successful STA Tus=1,filepath=newspost/2014-9-9/a.jpg*/functionUploadFile ($fileid,$dir,$maxsize=5242880,$exts=Array(' gif ', ' jpg ', ' jpeg ', ' BMP ', ' png '),$maxwidth=430){    $upload=New\think\upload ();//instantiating an upload class    $upload->maxsize =$maxsize;//set attachment upload size, per byte (picture limit 1M    $upload->exts =$exts;//set attachment upload type    $upload->rootpath = './uploads/';//Set attachments upload root directory    $upload->savepath =$dir.‘ /‘;//Set attachments Upload (sub) directory//upload file    $info=$upload-upload (); if(!$info) {//Upload error message        return Array(status=>0,msg=>$upload-GetError ()); }Else{//Upload Successful        return Array(status=>1,msg=> ' upload succeeded ',filepath=>$info[$fileid[' Savepath '].$info[$fileid[' Savename ']); }}

Here the default upload to the thinkphp Portal File index.php folder uploads, this method returns a data, the status Status=1 is a success, it is also recommended that when writing function modules or encapsulation, the whole system should have a contract at the beginning of the architecture, Returns the value in the form of an array, if necessary, and returns successfully

return Array (Status=>1,data=>....,info=> ...)

Can be returned when failed

Array (status->0,info=> ' can explain the cause of the error ',....)

This is in a unified manner conducive to normative development, team collaboration when looking at the code can improve efficiency, reduce the thinking of the operation, said far, the method of uploading the way to call the following:

//Excel File        if(!Empty($_files[' XLS '] [' Name '])){            $upload=uploadfile (' xls ', ' Tempxls ', 5242880,Array(' xls ', ' xlsx ')); if($upload[' Status ']){                $path=$upload[' FilePath ']; }Else{                $this->error ($upload[' Msg ']); }        }

Second, get Excel data

1. First, you need to introduce the Phpexcel class library

require_once ' module/phpexcel/classes/phpexcel/iofactory.php ';

2. Get Excel No. 0 sheet (SHEET1)

// Get Excel file $objPHPExcel = \phpexcel_iofactory::load ("uploads/$path"); $objPHPExcel->setactivesheetindex (0); $sheet 0=$objPHPExcel->getsheet (0);

3. Get the number of rows and read the data out $data array

$rowCount=$sheet 0->gethighestrow ();//number of Excel lines        $data=Array();  for($i= 2;$i<=$rowCount;$i++){            $item[' Name ']=$this->getexcelvalue ($sheet 0, ' A '.$i); $item[' Sex ']=$this->getexcelvalue ($sheet 0, ' B '.$i); $item[' Contact ']=$this->getexcelvalue ($sheet 0, ' C '.$i); $item[' Remark ']=$this->getexcelvalue ($sheet 0, ' D '.$i); $item[' Addtime ']=$this->getexcelvalue ($sheet 0, ' E '.$i); $data[]=$item; }

Third, last saved to data

$success=0; $error=0; $sum=Count($data); foreach($data  as $k=$v){            if(M (' Temp_area3 ')->data ($v),Add ()) {                $success++; }Else {                $error++; }        }        EchoTotal$sum} Bar, Success {$success} bar, failed {$errorReviews ";

To this end, simple ha.

Thinkphp Importing Excel Files (using Phpexcel)

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.