Home  >  Article  >  php教程  >  ThinkPHP实现excel的批量导入功能

ThinkPHP实现excel的批量导入功能

WBOY
WBOYOriginal
2016-06-07 11:44:361450browse

从网上找了半天excel的批量导入都没成功,在折腾了一上午之后终于做出来啦,现在贡献给大家,上代码
需要引入excel类,自己去下载,http://phpexcel.codeplex.com/releases/view/26478, 将Classes文件夹放入ThinkPHP\Extend\Vendor\位置。public function uploadFile(){<br>         $filetmpname = APP_PATH.'public/Uploads/624.xls';<br>         Vendor('Classes.PHPExcel');<br>         $objPHPExcel = PHPExcel_IOFactory::load($filetmpname);<br>         $arrExcel = $objPHPExcel->getSheet(0)->toArray();<br>         //删除不要的表头部分,我的有三行不要的,删除三次<br>         array_shift($arrExcel);<br>         array_shift($arrExcel);<br>         array_shift($arrExcel);//现在可以打印下$arrExcel,就是你想要的数组啦<br>         <br>         //查询数据库的字段<br>         $m = M('swt');<br>         $fieldarr = $m->query("describe kefu_swt");<br>         foreach($fieldarr as $v){<br>             $field[] = $v['Field'];<br>         }<br>         array_shift($field);//删除自动增长的ID        <br>         //循环给数据字段赋值<br>         foreach($arrExcel as $v){<br>             $fields[] = array_combine($field,$v);//将excel的一行数据赋值给表的字段<br>         }        <br>         //批量插入        <br>         if(!$ids = $m->addAll($fields)){<br>             $this->error("没有添加数据");<br>         }<br>         $this->success('添加成功');<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