Home >php教程 >php手册 >用phpexcel导入数据到数据库

用phpexcel导入数据到数据库

WBOY
WBOYOriginal
2016-06-07 11:41:271425browse

用phpexcel导入数据到数据库,自己写的,不知道有没有什么问题
<?php <br /> class ExcelAction extends Action {<br>     function index(){<br>     $this->display();<br>     <br>     }<br> <br>     function addexl(){<br>     import('ORG.Net.UploadFile');<br>     $upload = new UploadFile();// 实例化上传类<br>     $upload->maxSize  = 3145728 ;// 设置附件上传大小<br>     $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','xls');// 设置附件上传类型<br>     $upload->savePath =  './Public/Uploads/';// 设置附件上传目录<br> <br>     //$upload->saveRule = date('Ymdhm');<br>     if(!$upload->upload()) {<br>         $this->error($upload->getErrorMsg());    // 上传错误提示错误信息<br>     }else{<br>             $info =  $upload->getUploadFileInfo();// 上传成功 获取上传文件信息<br>             $this->success('上传成功');<br>             $file['pic_file']=$upload->savePath.$info[0]['savename'];//获取上传文件资源<br>             //echo $data['pic_file'];<br>     }<br>     Vendor("Classes.PHPExcel");//引入phpexcel类(注意你自己的路径)<br>     Vendor("Classes.PHPExcel.IOFactory");     <br>     Vendor("Classes.PHPExcel.Reader.Excel5"); <br>     Vendor("Classes.PHPExcel.Reader.Excel2007"); <br>     $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format <br>     $objPHPExcel = $objReader->load($file['pic_file']); <br>     $excelarray=$objPHPExcel->getsheet(0)->toArray();<br>     array_shift($excelarray);//将第一行移出数组<br>     unlink($file['pic_file']);//删除上传文件<br>     foreach($excelarray as $k=>$v){<br>         $data[$k]['sf']=$v[0];//$data[$k]['sf']对应数据库字段,$v[0]对应excel索引<br>         $data[$k]['ksh']=$v[1];<br>         $data[$k]['zkzh']=$v[2];<br>         $data[$k]['xm']=$v[3];<br>         $data[$k]['xbdm']=$v[4];<br>         $data[$k]['sfzh']=$v[5];<br>         $data[$k]['tdcj']=$v[6];<br>         $data[$k]['tdzy']=$v[7];<br>     }<br>     //dump($data);die();<br>     if($succ_result=M('date')->addAll($data)){<br>         echo "插入成功".$succ_result."条数据!!!<br>";<br>     }<br>     <br> }<br> }<br> ?>    

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn