您的位置:首页 > 编程语言 > PHP开发

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!');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息