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; }
原文地址:https://www.cnblogs.com/dereckbu/p/8447511.html
时间: 2024-10-08 11:19:09