This article mainly share with you TP3.2 Phpexcel Import Excel method, hope can help everybody.
Specific code:
Vendor (' Phpexcel.phpexcel ');p ublic function Excel_runimport () {$PHPExcel =new \phpexcel (); if (! empty ($_files [' file '] [' name ']) {$file _types = explode (".", $_files [' file '] [' name ']); $exts = $file _types [count ($file _types)-1]; /* discriminant is not an. xls file, discriminant is not an Excel file */if (Strtolower ($exts)! = "Xlsx" | | strtolower ($exts)! = "xls") {$ This->error (' Not Excel file, re-upload '); }//Generate a unique ID $filename = MD5 (Uniqid (Microtime (True), true)); $config =array (' maxSize ' =>70000000, ' exts ' =>array (' xlsx ', ' xls '), ' rootpath ' and ' = './uplo ads/excel/',//save filename ' savename ' = $filename,//Open subdirectory ' subn Ame ' =>array (' Date ', ' YMD '),); $upload =new \think\upload ($config); $info = $upload->upload (); if ($info) {if ($exts = = ' xls ') {import ("Vendor.PHPExcel.Reader.Excel5"); $PHPReader =new \phpexcel_reader_excel5 (); }else if ($exts = = ' xlsx ') {import ("Vendor.PHPExcel.Reader.Excel2007"); $PHPReader =new \phpexcel_reader_excel2007 (); } $rootPath = './uploads/excel/'; $savePath = $info [' file '] [' savepath ']; $saveName = $info [' file '] [' savename ']; Load file Creation Object $filePath = $rootPath. $savePath. $saveName; $objReader = $PHPReader->load ($filePath); Gets the first sheet in the table, if you want to get the second one, change 0 to 1, and so on $currentSheet = $objReader->getsheet (0); Gets the total number of columns $allColumn = $currentSheet->gethighestcolumn (); Gets the total number of rows $allRow = $currentSheet->gethighestrow (); Loops through the data in the table, $currentRow represents the current row, starting from which row to read the data, the index value starts at 0 $data = array ();//Create an empty array to receive tabular data//start with the first line of the loop for ($rowIndex =2; $rowIndex <= $allRow; $rowIndex + +) {//iterate to read the contents of each cell. Note that the row starts at 1, the column starts from A//Loop column for ($colIndex = ' A '; $colIndex <= $allColumn; $colIndex + +) { $addr = $colIndex. $rowIndex; $cell = $currentSheet->getcell ($ADDR)->getvalue (); if ($cell instanceof Phpexcel_richtext) {//Rich text conversion string $cell = $cell-> ; __tostring (); } $data [$rowIndex] [$colIndex] = $cell; }} if (Is_file ($filename)) unlink ($filename); }else{echo $upload->geterror (); }//$this->success (' Import Database succeeded ', U (' Excel_import '), 1); }}