search
Homephp教程PHP源码Thinkphp结合PHPExcel实现数据倒入

[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(&#39;DataFirstDiscipline&#39;);
                $secondDataMdl = M(&#39;DataSecondDiscipline&#39;);
                $first_discipline_code_backup = $first_discipline_code;//不去重复的备份
                $first_discipline_code = array_unique($first_discipline_code);//去重
                //一级学科导入
                foreach($first_discipline_code as $k=>$v){
                    $temp[&#39;college_id&#39;] = $college_code[$k];
                    $temp[&#39;first_discipline_code&#39;] = $v;
                    $temp[&#39;first_discipline_name&#39;] = $first_discipline_name[$k];
                    $firstDataMdl->add($temp);
                }
                //二级学科导入
                foreach($second_discipline_code as $k=>$v){
                    $temp[&#39;second_discipline_code&#39;] = $v;
                    $temp[&#39;second_discipline_name&#39;] = $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(&#39;first_discipline_id&#39;);
                    $temp[&#39;first_discipline_id&#39;] = $id;
                    $secondDataMdl->add($temp);
                }
                echo &#39;导入成功完成!&#39;;
            }catch (PHPExcel_Exception $e){
                echo $e->getMessage();
            }catch (ThinkException $e){
                echo $e->getMessage();
            }catch (Exception $e){
                echo &#39;程序异常,导入失败!&#39;;
            }
        }

                   

                   

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)