本文主要和大家分享TP3.2中phpexcel導入excel方法,希望能幫助到大家。
具體代碼:
Vendor('PHPExcel.PHPExcel'); public function excel_runimport(){ $PHPExcel=new \PHPExcel(); if (! empty ( $_FILES ['file'] ['name'] )){ $file_types = explode ( ".", $_FILES ['file'] ['name'] ); $exts = $file_types [count ( $file_types ) - 1]; if (strtolower ( $exts ) != "xlsx" || strtolower ( $exts ) != "xls"){ $this->error ( '不是Excel文件,重新上傳'); } //生成唯一的ID $filename = md5(uniqid(microtime(true),true)); $config=array('maxSize'=>70000000, 'exts'=>array('xlsx','xls'), 'rootPath'=>'./Uploads/excel/', //保存的文件名 'saveName' =>$filename, //開啟子目錄 'subName' =>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']; //加載文件創建對象 $filePath=$rootPath.$savePath.$saveName;$objReader = $PHPReader->load($filePath); //獲取表中的第一個工作表,如果要獲取第二個,把0改為1,依次類推 $currentSheet=$objReader->getSheet(0); //獲取總列數 $allColumn=$currentSheet->getHighestColumn(); //獲取總行數 $allRow=$currentSheet->getHighestRow(); //循環獲取表中的數據,$currentRow表示當前行,從哪行開始讀取數據,索引值從0開始 $data = array();//創建空數組接收表格數據 //從第幾行開始循環 for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){ //循環讀取每個單元格的內容。注意行從1開始,列從A開始 //循環列 for($colIndex='A';$colIndex<=$allColumn;$colIndex++){ $addr = $colIndex.$rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); if($cell instanceof PHPExcel_RichText){ //富文本轉換字符串 $cell = $cell->__toString(); } $data[$rowIndex][$colIndex] = $cell; } } if(is_file($filename)) unlink($filename); }else{ echo $upload->getError(); } // $this->success ('導入數據庫成功',U('excel_import'),1); } }
相關推薦:
手把手教你mvc導入excel_實用技巧
phpexcel如何導入excel處理大數據的代碼實例
(進階篇)使用PHP導入Excel和導出數據為Excel文件
以上就是TP3.2中phpexcel導入excel方法分享的詳細內容,更多請關注php中文網其它相關文章!