博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 导入Excel数据 到数据库
阅读量:6816 次
发布时间:2019-06-26

本文共 3425 字,大约阅读时间需要 11 分钟。

/**     * 导入excel     * @throws \PHPExcel_Exception     * @throws \PHPExcel_Reader_Exception     */    public function impExcel($filename,$data){        if (!empty($_FILES)) {            $config = array(                'exts'=>array('xlsx','xls'),                'rootPath'=>"./",                'savePath'=> "Public/Uploads/$filename/",                'subName'    =>    array('date','Ymd'),            );            $upload = new \Think\Upload($config);            if (!$info=$upload->upload()) {                $this->error($upload->getError());            }            vendor("PHPExcel.PHPExcel");            $file_name = $info['excel']['savepath'].$info['excel']['savename'];            //$objReader = \PHPExcel_IOFactory::createReader('Excel5');            $Excel_name = getcwd() . '/' .$file_name;            //$aaa = is_readable($Excel_name);           // $objPHPExcel = $objReader->load($Excel_name,$encode='utf-8');            $extension = strtolower( pathinfo($Excel_name, PATHINFO_EXTENSION) );            if ($extension =='xlsx') {                $objReader = new \PHPExcel_Reader_Excel2007();                $objPHPExcel = $objReader ->load($Excel_name);            } else if ($extension =='xls') {                $objReader = new \PHPExcel_Reader_Excel5();                $objPHPExcel = $objReader ->load($Excel_name);            } else if ($extension=='csv') {                $PHPReader = new \PHPExcel_Reader_CSV();                //默认输入字符集                $PHPReader->setInputEncoding('GBK');                //默认的分隔符                $PHPReader->setDelimiter(',');                //载入文件                $objPHPExcel = $PHPReader->load($Excel_name);            }            $sheet = $objPHPExcel->getSheet(0);            $highestRow = $sheet->getHighestRow(); // 取得总行数            $highestColumn = $sheet->getHighestColumn(); // 取得总列数            for($i=2;$i<=$highestRow;$i++)            {                $data['name'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();                $data['tel'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();                $data['zjh']    = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();                $data['sheng'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();                $data['shi'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();                $data['address']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();                $data['money']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();                $data['hk_time']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();                $data['nanyi']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();                $data['uid'] = $_SESSION['user']['id'];                $data['time'] = date("Y-m-d H:i:s");                M('release')->add($data);            }            $this->success('导入成功!');exit();        }else        {            $this->error("请选择上传的文件");        }    }
调用:
if(IS_POST)        {            $data['reason'] = I("post.reason","","trim");            $data['type'] = I("post.type","","trim");           // $data['qixian'] = I("post.qixian","","trim");           // $data['comm'] = I("post.comm","","trim");            if(!empty($_FILES)){                $this->impExcel('batch_release',$data);            }        }

转载于:https://www.cnblogs.com/ningjiabing/p/10066091.html

你可能感兴趣的文章
Eclipse 使用maven创建Dynamic Web Project
查看>>
Python学习笔记——迭代器(RandSeq和AnyIter)
查看>>
MySQL索引使用方法和性能优化
查看>>
JSP简单练习-定时刷新页面
查看>>
JSON.parse()与JSON.stringify()的区别
查看>>
[Python] Boolean Or "Mask" Index Arrays filter with numpy
查看>>
有了#ifdef 为什么还需要#if defined
查看>>
eclipse中.properties文件不能输入中文的解决办法
查看>>
[Unit Testing] Mock a Node module's dependencies using Proxyquire
查看>>
C++中malloc/free和new/delete 的使用
查看>>
ASP.NET MVC读取XML并使用ViewData显示
查看>>
4.lists(双向链表)
查看>>
导入项目的时候报错Error:Could not find com.android.support.constraint:constraint-layout:1.0.0-alpha7...
查看>>
微服务(Microservices )简介
查看>>
.NET中的流
查看>>
在ASP.NET MVC 4中使用Kendo UI Grid
查看>>
TCP/IP四层模型
查看>>
Oracle用户管理的不完全恢复2:基于取消的恢复
查看>>
程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大经典原创系列集锦与总结...
查看>>
poj 2828 Buy Tickets 【线段树点更新】
查看>>