ThinkPHP3.2.3利用PHPExcel完成数据导出导入功能

  • 4,522
摘要

以下导出导入功能分两次项目结合完成!希望看到本篇文章的朋友不要忽略PHPExcel文件的路径!

ThinkPHP3.2.3利用PHPExcel完成数据导出导入功能

导出功能

public function lst(){
        $formModel = D('form');
        $arr = $formModel -> order('id desc')-> select();
        $a2=array(
                array("ID","学校","年级","姓名","邮箱","出生年月","民族","身份证号","学籍","参赛项目","足球","篮球","排球","乒乓球","田径","游泳","羽毛球","证件照(正)","证件照(反)","最新个人照"),
                );
        $as = array_merge_recursive($a2,$arr);
        import("Org.Util.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $objSheet = $objPHPExcel -> getActiveSheet();
        $objSheet->setTitle("demo");

        $array = $as;
        $objSheet->fromArray($array);
        /*$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");*/
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="山东省学校体育比赛资格审查登记表.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

    }

ThinkPHP3.2.3利用PHPExcel完成数据导出导入功能

导入功能

                            <form class="am-form am-u-sm-12" method="post" action="{:U('zijin/impUser')}" enctype="multipart/form-data">
                                <div class="am-form-group">
                                     <select name="table">
                                    <foreach name="fenleilst" item="vo">
                                    <option value="{$vo.id}">{$vo.xiangmuming}</option>
                                    </foreach>                                 
                                </select>
                                </div>
                                <div class="am-form-group am-form-file">
                                  <button type="button" class="am-btn am-btn-danger am-btn-sm">
                                    <i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
                                  <input id="doc-form-file" name="excel" type="file" multiple>
                                </div>
                                <div id="file-list"></div>
                                <button type="submit" class="am-btn am-btn-primary">提交</button> 
                            </form>
       /**实现导入excel**/
   public function impUser(){
        if(!empty($_FILES)){
            $table = I('post.table');
            if(!$table){
                $this->error('请选择要上传的栏目');
            }
            $config=array(
                'exts'=>array('xlsx','xls'),
                'rootPath'=>"./Public/",
                'savePath'=>'Uploads/',
                //'autoSub'    =>    true,
                'subName'    =>    array('date','Ymd'),
            );
            $upload = new \Think\Upload($config);
            //var_dump($upload);exit;
            
            if (!$info=$upload->uploadone($_FILES['excel'])) {
                $this->error($upload->getError());
            } 
            
            
            //var_dump($_FILES);exit;
            vendor("PHPExcel.PHPExcel");
            $file_name=$upload->rootPath.$info['savepath'].$info['savename'];
          
            //var_dump($file_name);exit;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $highestColumn = $sheet->getHighestColumn(); // 取得总列数
            if($table != ''){
                for($i=1;$i<=$highestRow;$i++)
                {
                    $data['quhua']= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
                    $data['xiangzhen']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    $data['cun'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                    $data['zu'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $data['xingming'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                    $data['shenfen']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                    $data['zhuzhi']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                    $data['butie'] = $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                    $data['renkou'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                    $data['biaozhun'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
                    $data['wunong']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
                    $data['wugong'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
                    $data['mushu'] = $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
                    $data['dianhua'] = $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
                    $data['yiben'] = $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue(); 
                    $data['time'] = date('Y-m-d');
                    $data['timey'] = date('Y');
                    $data['cateid'] = I('post.table');

                    $arr = D(zijin);
                    $add = $arr->add($data);   
                }
            }
            $this->success('导入成功!');
        }else
        {
            $this->error("请选择上传的文件");
        }
            

    }

ThinkPHP3.2.3利用PHPExcel完成数据导出导入功能

weinxin
我的微信
这是我的微信扫一扫
开拓者博主
  • 本文由 发表于 2017年7月8日16:12:23
  • 转载请务必保留本文链接:https://www.150643.com/454.html
匿名

发表评论

匿名网友 填写信息

评论:5   其中:访客  3   博主  2
    • 二次元空间 二次元空间 0

      谢谢博主无私的分享好东西,留言支持下,欢迎回访我的网站,如果觉得我的网站不错的话能否交换个友链

      • 加气块设备 加气块设备 1

        好专业

        • 素材火 素材火 0

          实用的干货