Home > php教程 > php手册 > PHPExcel类添加excel文件中的数据到Mysql中

PHPExcel类添加excel文件中的数据到Mysql中

WBOY
Release: 2016-06-07 11:40:37
Original
947 people have browsed it

如题
不说别的了,直接上代码。本人是个菜菜,刚学thinkphp框架,请各位高手多多指教!!测试过可以添加,不知道文件太大了效率怎么样。<?php <br /> // 本类由系统自动生成,仅供测试用途<br> class IndexAction extends Action {<br>     public function index(){<br>         $this->display();<br>     }<br> <br>     Public function upload(){<br>         import('ORG.Net.UploadFile');<br>         $upload = new UploadFile();// 实例化上传类<br>         $upload->maxSize  = 3145728 ;// 设置附件上传大小<br>         $upload->allowExts  = array('xls','xlsx','csv');// 设置附件上传类型<br>         $upload->savePath =  './Public/Uploads/';// 设置附件上传目录<br>          if(!$upload->upload()) {// 上传错误提示错误信息<br>         $this->error($upload->getErrorMsg());<br>          }else{// 上传成功 获取上传文件信息<br>         $info =  $upload->getUploadFileInfo();<br>          }<br>         <br>          //加载第三方类库PHPExcel<br>          vendor('phpexcel.PHPExcel');<br>          vendor('phpexcel.PHPExcel.IOFactory');<br>          vendor('phpexcel.PHPExcel.Reader.Excel5');<br>          vendor('phpexcel.PHPExcel.Reader.Excel2007');<br>          <br>          // 加载excel文件<br>          $inputfilename=$info[0]['savepath'].$info[0]['savename'];<br>          $extension=$info[0]['extension'];<br>          switch ($extension) {<br>              case 'xls':<br>                  $objReader = new PHPExcel_Reader_Excel5();<br>                  break;<br>              case 'xlsx':<br>                  $objReader = new PHPExcel_Reader_Excel2007();<br>                  break;<br>              case 'csv':<br>                  $objReader = new PHPExcel_Reader_CSV();<br>                  break;<br>              default:<br>                  $this->error('上传的文件类型不匹配');<br>                  break;<br>              }<br>          <br>          $objPHPExcel = $objReader->load($inputfilename);<br>          $currentsheet = $objPHPExcel->getActiveSheet();//获取活动工作薄<br>          $allcolumn = $currentsheet->getHighestColumn();//获取最大列数<br>          $allrow = $currentsheet->getHighestRow();//获取最大行数<br>          $allcolumnindex = PHPExcel_Cell::columnIndexFromString($allcolumn);//将列数的字母索引转换成数字<br>          //建立模型<br>          $data = M('data');<br>          //读取excel文件中的数据<br>          for($row=2;$row              $str=array();<br>              for($column=0;$column                  $str[$column]=$currentsheet->getCellByColumnAndRow($column,$row)->getValue();                 <br>              }<br>              //赋值到数组中<br>              $record=array(<br>                  'username' => $str[0],<br>                  'password' => $str[1],<br>                  'idcard' => $str[2],<br>                  );<br>             //把数据插入到数据库中<br>              $data->create($record);<br>              $data->add();<br>          }<br> <br>      }<br> }

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template