Home > php教程 > PHP源码 > Thinkphp结合PHPExcel实现数据倒入

Thinkphp结合PHPExcel实现数据倒入

PHP中文网
Release: 2016-05-25 16:59:47
Original
1348 people have browsed it

[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;;
            }
        }
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template