/** * Excel导入函数 * @author crx349 */ if (!empty($_FILES)) { $config = array( 'maxSize' => 3145728, 'rootPath' => './Uploads/', 'savePath' => 'Excel/', 'saveName' => array('uniqid',''), 'exts' => array('xlsx','csv','xls'), 'autoSub' => true, 'subName' => array('date','Ymd'), ); $upload = new \Think\Upload($config); $info = $upload->upload(); if(!$info) { $this->error($upload->getError()); }else{ foreach($info as $file) { $file_name = $config['rootPath'].$file['savepath'].$file['savename']; } } Vendor("PHPExcel"); $extension = substr(strrchr($file_name,"."),1); if( $extension =='csv' ) { $objReader = \PHPExcel_IOFactory::createReader('CSV'); } else { $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); } $objPHPExcel = $objReader->load($file_name,$encode='utf-8'); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 //第二行A列起 for($i=2;$i<=$highestRow;$i++) { $data['customername']= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//姓名 $data['customercode']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();//编号 $data['phone']= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();//手机号 //$data['course_id']= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();//科目 //$data['number']= '0';//默认号数 M('WcoaUCustomers')->add($data); } $this->success('导入成功!',U('customer_daoru')); }else { $this->error("请选择上传的文件"); }
1、将Classes下的文件放到\ThinkPHP\Library\Vendor下
2、csv格式需要将csv.php中的编码格式改为'gb2312',要不然汉字无法读取