码迷,mamicode.com
首页 > Web开发 > 详细

PHP Excel导入 阿星小栈

时间:2018-02-13 22:54:25      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:ase   create   key   xlsx   dex   har   unicode   ksh   set   

 

   public function doOrderIn(){

        $filename = public_path("/question.xlsx");

        if ($filename)
        {
            $tmp_file = $filename;

            $file_types = explode ( ".", $filename);

            $file_type = $file_types [count ( $file_types ) - 1];

            $objReader = \PHPExcel_IOFactory::createReaderForFile($filename);

            $objPHPExcel = $objReader->load($filename);

            $objPHPExcel->setActiveSheetIndex();

            $date = $objPHPExcel->getActiveSheet()->getCell()->getValue();

            $objWorksheet = $objPHPExcel->getActiveSheet();

            $i = 0;
            foreach($objWorksheet->getRowIterator() as $row){

                $cellIterator = $row->getCellIterator();

                $cellIterator->setIterateOnlyExistingCells(false);

                foreach($cellIterator as $cell){

                    $data[$i][] = $cell->getValue();
                }
                $i++;
            }
            unset($data[0]);
            unset($data[1]);
            unset($data[2]);

        }
        $updata = [];

        $key = -1;

        $counts = count($data);

        $succNum = 0;
        $data1 = [];
        if ($data) {

            foreach ($data as $k=>$v){

                $data1[$k][‘difficulty‘] = $this->getDifficultyText($v[2]);

                $data1[$k][‘title‘] = $v[3];

                $content = [];
                $content[] = $v[4];
                $content[] = $v[5];
                $content[] = $v[6];
                $content[] = $v[7];

                $data1[$k][‘contents‘] = json_encode($content,JSON_UNESCAPED_UNICODE);

                $data1[$k][‘answer‘] = $this->getAnswerText($v[8]);

                $data1[$k][‘question_type_text‘] = $v[9];

                $data1[$k][‘source_from‘] = $v[10];

                $data1[$k][‘author‘] = $v[11];

                $data1[$k][‘is_listed‘] = 1;

            }

        }
        foreach($data1 as $k=>$v){
            $res = Question::create($v);
            if($res){
               $succNum++;
            }
        }
        $cou = count($data1);
        $dis = $cou - $succNum;
        echo "共".$cou."条,成功".$succNum."条,失败".$dis;

    }
    public function getDifficultyText($type){
        switch ($type){
            case ‘A‘:{
                $text = 1000;
                break;
            }
            case ‘B‘:{
                $text = 2000;
                break;
            }
            case ‘C‘:{
                $text = 3000;
                break;
            }
            case ‘D‘:{
                $text = 4000;
                break;
            }
            default:{
                $text = 1000;
            }
        }
        return $text;
    }
    public function getAnswerText($type){
        switch ($type){
            case ‘A‘:{
                $text = 0;
                break;
            }
            case ‘B‘:{
                $text = 1;
                break;
            }
            case ‘C‘:{
                $text = 2;
                break;
            }
            case ‘D‘:{
                $text = 3;
                break;
            }
            default:{
                $text = 0;
            }
        }
        return $text;
    }

  

PHP Excel导入 阿星小栈

标签:ase   create   key   xlsx   dex   har   unicode   ksh   set   

原文地址:https://www.cnblogs.com/dereckbu/p/8447511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!