PHP Excel导入 阿星小栈

Posted tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Excel导入 阿星小栈相关的知识,希望对你有一定的参考价值。

 

   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导入 阿星小栈的主要内容,如果未能解决你的问题,请参考以下文章

php根据ID生成员工编号 阿星小栈

php获取中文字符拼音首字母 阿星小栈

PHP数组分割成新数组 阿星小栈

php 将base64转换成图片并保存 阿星小栈

PHP获取文件后缀名(提供7种方法) 阿星小栈

百度地图 sn 计算方法 阿星小栈