欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Thinkphp结合PHPExcel实现数据倒入

程序员文章站 2022-04-02 15:07:53
...

[PHP]代码

        set_time_limit(0);//防止数据量过大超时
        //处理进程
        import('ORG.Net.UploadFile');

        $upload = new UploadFile(); // 实例化上传类

        $upload->maxSize = 4 * 1024 * 1024; // 设置附件上传大小

        $upload->allowExts = array('xls');

        $upload->savePath = './Upload/data/'; // 设置附件上传目录

        $upload->saveRule = date('YmdHis');

        if (!$upload->upload()) { // 上传错误提示错误信息
           echo $upload->getErrorMsg();
        } else {
            //进行异常捕获,防止导入错误
            try{
                //导入完成,开始数据处理流程
                $info = $upload->getUploadFileInfo();
                $data = $info[0];
                $path = $data['savepath'].$data['savename'];

                /**
                 * 开始Excel处理进程
                 */
                vendor('PHPExcel.PHPExcel');
                $objReader  =   new PHPExcel_Reader_Excel5();
                $objExcel   =   $objReader->load($path);
                $objSheet   =   $objExcel->getActiveSheet();
                $rows       =   $objSheet->getHighestRow();//行数
                /**
                 * 单元格定义
                 * B 二级学科代码
                 * C 二级学科名称
                 * D 一级学科代码
                 * E 一级学科名称
                 * F 学院代码
                 * G 学院名称
                 */
                //定义数组
                $second_discipline_code = array();
                $second_discipline_name = array();
                $first_discipline_code = array();
                $first_discipline_name = array();
                $college_code = array();
                $college_name = array();
                for($i=1;$i<=$rows;$i++){
                    $second_discipline_code[]  =   $objSheet->getCell("B$i")->getFormattedValue();
                    $second_discipline_name[]  =   $objSheet->getCell("C$i")->getFormattedValue();
                    $first_discipline_code[]   =   $objSheet->getCell("D$i")->getFormattedValue();
                    $first_discipline_name[]   =   $objSheet->getCell("E$i")->getFormattedValue();
                    $college_code[]   =   $objSheet->getCell("F$i")->getFormattedValue();
                    $college_name[]   =   $objSheet->getCell("G$i")->getFormattedValue();
                }
                //写入数据库
                $firstDataMdl  = M('DataFirstDiscipline');
                $secondDataMdl = M('DataSecondDiscipline');
                $first_discipline_code_backup = $first_discipline_code;//不去重复的备份
                $first_discipline_code = array_unique($first_discipline_code);//去重
                //一级学科导入
                foreach($first_discipline_code as $k=>$v){
                    $temp['college_id'] = $college_code[$k];
                    $temp['first_discipline_code'] = $v;
                    $temp['first_discipline_name'] = $first_discipline_name[$k];
                    $firstDataMdl->add($temp);
                }
                //二级学科导入
                foreach($second_discipline_code as $k=>$v){
                    $temp['second_discipline_code'] = $v;
                    $temp['second_discipline_name'] = $second_discipline_name[$k];
                    //根据一级学科代码查一级学科ID
                    $first_discipline_code_flag = $first_discipline_code_backup[$k];
                    $id = $firstDataMdl->where("first_discipline_code = %d",$first_discipline_code_flag)->getField('first_discipline_id');
                    $temp['first_discipline_id'] = $id;
                    $secondDataMdl->add($temp);
                }
                echo '导入成功完成!';
            }catch (PHPExcel_Exception $e){
                echo $e->getMessage();
            }catch (ThinkException $e){
                echo $e->getMessage();
            }catch (Exception $e){
                echo '程序异常,导入失败!';
            }
        }
相关标签: php