検索
ホームページphp教程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;;
            }
        }

                   

                   

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。