ThinkPHP3.2---excel导入mysql
2016-02-21 00:59
543 查看
1.最终效果和Html代码
<div> 【1】 </div> <div> <a href="__PUBLIC__/Admin/Files/model.xlsx" title="下载模板" class="easyui-linkbutton c5" style="width:120px;height:40px;" onclick="">下载模板</a> </div> <p> <div> 【2】 </div> <!-- 1.上传文件到服务器 --> <form action="__URL__/upload" enctype="multipart/form-data" method="post" > <a href="javascript:void(0);" class="easyui-linkbutton c5 file" style="width:120px;height:40px;" onclick="">选择文件 <input type="file" name="photo" class="easyui-linkbutton c5"/> </a> <p> <div> 【3】 </div> <input type="submit" value="开始导入" class="easyui-linkbutton c5" style="width:120px;height:40px;" > </form>
2.配置phpexcel
下载PHPExcel 1.8.0(http://phpexcel.codeplex.com/)解压到\ThinkPHP\Library\Vendor目录下,并改目录名为PHPExcel,该目录下结构为:
3.Excel文件上传到服务器
publicfunction upload(){ $upload=new \Think\Upload(); //实例化上传类 $upload->maxSize = 3145728; //设置附件上传大小 $upload->exts = array('xlsx','xls'); //设置附件上传类型 $upload->rootPath = './Uploads/'; //设置附件上传根目录(没有则需手动新建) $upload->savePath = ''; //设置附件上传(子)目录 //上传文件 $info = $upload->upload(); if(!$info){ //上传错误提示错误信息 $this->error($upload->getError()); }else{ //上传成功获取上传文件信息 foreach($infoas$file){ //echo $file['savepath'].$file['savename']; //这里的路径为网站根目录下Uploads/2016-02-19/56c7056f732ff.xlsx $filePath="Uploads/".$file['savepath'].$file['savename']; //等价于 //$filePath = __ROOT__."Uploads/" .$file['savepath'].$file['savename']; } echo "<h3>".$filePath."文件上传成功!</h3><p>"; //上传成功则开始导入到mysql中 $result=$this->importExcel($filePath); echo $result['message']; } }
4.将上传到服务中的Excel文件数据导入到mysql数据库表中
public function importExcel($file){ if(!file_exists($file)){ return array("error"=>0,'message'=>'file not found!'); } vendor("PHPExcel.Classes.PHPExcel.IOFactory"); //出现:Class 'Admin\Controller\PHPExcel_IOFactory' not found //注意这儿加了一个"\"表示调用公共空间,也可以理解为顶层 //$objReader = \PHPExcel_IOFactory::createReader('Excel5'); //获取excel文件:获取Excel第1张表即(Sheet1) $objPHPExcel= \PHPExcel_IOFactory::load($file); $objPHPExcel->setActiveSheetIndex(0); $sheet1=$objPHPExcel->getSheet(0); //获取行数,并把数据读取出来$data数组 $rowCount=$sheet1->getHighestRow();//excel行数 $data=array(); for($i=2;$i<=$rowCount;$i++){ $item['id']=$sheet1->getCellByColumnAndRow(0,$i)->getValue(); $item['login_name']=$sheet1->getCellByColumnAndRow(1,$i)->getValue(); $item['name']=$sheet1->getCellByColumnAndRow(2,$i)->getValue(); $item['passwd']=$sheet1->getCellByColumnAndRow(3,$i)->getValue(); $item['enabled']='1'; $item['phone']=$sheet1->getCellByColumnAndRow(5,$i)->getValue(); $item['birthday']=$sheet1->getCellByColumnAndRow(6,$i)->getValue(); $item['vip_level']=$sheet1->getCellByColumnAndRow(7,$i)->getValue(); $item['balance']=$sheet1->getCellByColumnAndRow(8,$i)->getValue(); $item['address']=$sheet1->getCellByColumnAndRow(9,$i)->getValue(); $item['shop_code']=$sheet1->getCellByColumnAndRow(10,$i)->getValue(); $item['register_date']=date("Y-m-d"); $item['last_consumption_date']=date("Y-m-d H:i:s"); $item['remark']=$sheet1->getCellByColumnAndRow(13,$i)->getValue(); $item['consumption_total']=$sheet1->getCellByColumnAndRow(14,$i)->getValue(); $data[]=$item; } $success=0; $error=0; $sum=count($data); foreach($dataas$k=>$v){ if(M('t_vip')->data($v)->add()){ $success++; }else{ $error++; } } echo "<h3>总{$sum}条,成功{$success}条,失败{$error}条</h3>"; return array("error"=>0,'message'=>'import is succussful!'); }
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- 使用Python生成Excel格式的图片
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志
- MySQL 安全事宜