The example in this article describes how to import an Excel file based on Phpexcel thinkphp. Share to everyone for your reference. Here's how:
The main point of knowledge, using Phpexcel to import Excel data after these days of testing or can, XLS,XLSX can get Excel data.
Download Address: http://phpexcel.codeplex.com/
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.
The
copy Code code is as follows:/**
* TODO Upload file Method
* @param $fileid name value of form form file
* @param $dir uploaded to Uploads $dir folder
* @param int $maxsize maximum upload limit, default 1024000 byte
* @param array $exts allow upload file type default array (' GIF ', ' jpg ', ' jpeg ', ' B MP ', ' png ')
* @return Array returned array, failed status=0 successful status=1,filepath=newspost/2014-9-9/a.jpg
*/
function UploadFile ($fileid, $dir, $maxsize =5242880, $exts =array (' gif ', ' jpg ', ' jpeg ', ' BMP ', ' png '), $maxwidth =430) {
$ Upload = new \think\upload ();//Instantiate upload class
$upload->maxsize = $maxsize;//Set attachment upload size, per byte (picture limit 1M
$upload->exts = $exts;//Set the attachment upload type
$upload->rootpath = './uploads/';//Set attachments to upload the root directory
$upload->savepath = $dir. '/';//Set Attachments upload (sub) Directory
//upload file
$info = $upload->upload ();
if (! $info) {//Upload error error message
Return Array (status=>0,msg=> $upload->geterror ());
}else{//Upload Success
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
Copy the code as follows: return array (STATUS=>1,DATA=>....,INFO=> ...)
Can be returned when failed
Copy the code as follows: 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:
Copy the Code code as follows://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
Copy the Code code as follows: require_once ' module/phpexcel/classes/phpexcel/iofactory.php ';
2. Get Excel No. 0 sheet (SHEET1)
Copy the Code Code as follows://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
Copy the Code code as follows: $rowCount = $sheet 0->gethighestrow ();//excel line number
$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 save to database
Copy the Code code as follows: $success = 0;
$error = 0;
$sum =count ($data);
foreach ($data as $k = = $v) {
if (M (' Temp_area3 ')->data ($v)->add ()) {
$success + +;
}else {
$error + +;
}
}
echo "Total {$sum}, success {$success} bar, failed {$error}. ";
That's it! It is hoped that this article will be helpful to everyone's thinkphp framework design.