setReadDataOnly(true); while(1){ $r=M('import')->limit('0,1')->order('id desc')->where(array('status'=>0))->find(); if($r){ $json=json_decode($r['json'],1); exec('unzip -o ./Uploads/zip/'.$json['file'].' -d ./Uploads/tmp/'); $objPHPExcel = $reader->load($path); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $arr=array(); $err=array(); foreach($sheetData as $k=>$v){ if($k<=1)continue; if(!$v['A'])continue; $data=array(); $edit=0; $data['course_id'] = $json['course_id']; $data['words_name']=$v['A']; $info = M('Words')->where(array('words_name'=>$data['words_name'],'course_id'=>$json['course_id']))->find(); if($info){ $edit=$info['words_id']; //$err[]=$data['words_name'].'已经存在'; } $info=$this->dict($data['words_name']); if($info['code']==0){ foreach($info['data']['ps'] as $m=>$n){ if($m>2)break; $data['words_read'.($m+1)]='['.$n.']'; } foreach($info['data']['pron'] as $m=>$n){ if($m>2)break; $data['words_voice'.($m+1)]=$n; } foreach($info['data']['pos'] as $m=>$n){ $data['words_'.$n[0]]=$n[1]; } foreach($info['data']['sent'] as $m=>$n){ if($m>2)break; $data['jz_en'.($m+1)]=$n[0]; $data['jz_cn'.($m+1)]=$n[1]; } } $data['words_similar']=$v['B']; $data['similar_cn']=$v['C']; $data['annotation_cn']=$v['D']; if($v['E'])$data['words_n']=$v['E']; if($v['F'])$data['words_pron']=$v['F']; if($v['G'])$data['words_adj']=$v['G']; if($v['H'])$data['words_num']=$v['H']; if($v['I'])$data['words_v']=$v['I']; if($v['J'])$data['words_adv']=$v['J']; if($v['K'])$data['words_art']=$v['K']; if($v['L'])$data['words_prep']=$v['L']; if($v['M'])$data['words_conj']=$v['M']; if($v['N'])$data['words_int']=$v['N']; if($v['O'])$data['words_vt']=$v['O']; if($v['P'])$data['words_vi']=$v['P']; if($v['Q'])$data['words_other']=$v['Q']; if($v['R'])$data['jz_en1']=$v['R']; if($v['S'])$data['jz_cn1']=$v['S']; if($v['T'])$data['jz_en2']=$v['T']; if($v['U'])$data['jz_cn2']=$v['U']; if($v['V'])$data['jz_en3']=$v['V']; if($v['W'])$data['jz_cn3']=$v['W']; $t_path='./Uploads/tmp/img/'.$data['words_name']; if (is_dir($t_path)){ $t_arr=array(); $files = scandir($t_path); foreach ($files as $file) { if($file != '.' && $file != '..'){ $info = pathinfo($t_path.'/'.$file); $ext_suffix = $info['extension']; $new_filename = date('YmdHis',time()).rand(100,1000).'.'.$ext_suffix; $new_dir='Uploads/'.date('Y-m-d',time()).'/'; if (!is_dir($new_dir)){ mkdir($new_dir,0777,true); } rename($t_path.'/'.$file,$new_dir.$new_filename); $t_arr[]="http://www.hanlinyuanonline.com/".$new_dir.$new_filename; } } $data['words_img']=implode(',',$t_arr); } //if($err)continue; if($edit){ M('Words')->where(array('words_id'=>$edit))->save($data); }else{ $wordsId = M('Words')->add($data); M('Course')->where(array('course_id'=>$json['course_id']))->setInc('course_num'); } } $arr=array('status'=>1,'note'=>'导入成功'); if($err){ $arr['note']="导入失败,原因如下:\n".implode(',',$err); } M('import')->where(array('id'=>$r['id']))->save($arr); exec('rm -rf ./Uploads/tmp/*'); } sleep(1); } } //获取单词信息接口 private function dict($wordsName){ $wordsFields = array('n','pron','adj','num','v','adv','art','prep','conj','int','vt','vi','other',); $httpCurl = new \Org\Net\HttpCurl(); $url = C('TRANS').'&w='.$wordsName; $res = $httpCurl->get($url,'xml'); $res = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true); /* $url = C('TRANS').'&type=json&w='.$wordsName; $res = $httpCurl->get($url); */ if (!isset($res['key'])){ return array('code'=>1); } if (isset($res['ps'])){ if (!is_array($res['ps'])){ $data['data']['ps'][] = $res['ps']; }else{ $data['data']['ps'] = $res['ps']; } }else{ $data['data']['ps'] = array(); } if (isset($res['pron'])){ if (!is_array($res['pron'])){ $data['data']['pron'][] = $res['pron']; }else{ $data['data']['pron'] = $res['pron']; } /* $data['data']['pron'] = array( 'http://res.iciba.com/resource/amp3/0/0/c4/7d/c47d187067c6cf953245f128b5fde62a.mp3', 'http://res.iciba.com/resource/amp3/0/0/34/d1/34d1f91fb2e514b8576fab1a75a89a6b.mp3' ); */ }else{ $data['data']['pron'] = array(); } if (isset($res['pos']) && !empty($res['pos'])){ if (is_array($res['pos'])){ foreach ($res['pos'] as $k=>$v){ $key = str_replace('.', '', $v); if (!in_array($key, $wordsFields)){ $pos[] = array('other', trim($res['acceptation'][$k])); }else{ $pos[] = array($key, trim($res['acceptation'][$k])); } } }else{ $key = str_replace('.', '', $res['pos']); //$pos[] = array(str_replace('.', '', $res['pos']),trim($res['acceptation'])); if (!in_array($key, $wordsFields)){ $pos[] = array('other', trim($res['acceptation'])); }else{ $pos[] = array($key, trim($res['acceptation'])); } /* if (isset($wordsKeys[str_replace('.', '', $res['pos'])])){ $pos[] = array(str_replace('.', '', $res['pos']),trim($res['acceptation'])); }else{ $pos[] = array('words_other',trim($res['acceptation'])); } */ } $data['data']['pos'] = $pos; }else{ $pos[] = array('other', trim($res['acceptation'])); $data['data']['pos'] = $pos; } if (isset($res['sent'])){ if (empty($res['sent'][0])){ $sent[] = array(trim($res['sent']['orig']),trim($res['sent']['trans'])); }else{ foreach ($res['sent'] as $k=>$v){ if ($k>2){ continue; } $sent[] = array(trim($v['orig']),trim($v['trans'])); } } $data['data']['sent'] = $sent; } $data['code'] = 0; return $data; } public function export(){ while(1){ //$this->excel('course'); exec("rm -rf export/*"); $course = M('course')->where(array('is_delete'=>1,'course_source'=>1,'course_flag'=>1))->select(); foreach($course as $k=>$v){ $path='export/'.$v['course_name']; mkdir($path,0777,true); mkdir($path.'/img',0777,true); $words=M('words')->field('words_name,words_similar,similar_cn,annotation_cn,words_n,words_pron,words_adj,words_num,words_v,words_adv,words_art,words_prep,words_conj,words_int,words_vt,words_vi,words_other,jz_en1,jz_cn1,jz_en2,jz_cn2,jz_en3,jz_cn3,words_img')->where(array('is_delete'=>1,'course_id'=>$v['course_id']))->select(); foreach($words as $m=>$n){ $words[$m]['words_name']=$n['words_name']=htmlspecialchars_decode($n['words_name']); if($n['words_img']){ $img_path=$path.'/img/'.$n['words_name']; mkdir($img_path,0777,true); $tmp=explode(',',$n['words_img']); foreach($tmp as $x=>$y){ $file=str_replace('http://www.hanlinyuanonline.com/','',$y); $info = pathinfo($file); $ext_suffix = $info['extension']; copy($file,$img_path.'/'.$n['words_name'].($x+1).'.'.$ext_suffix); } } unset($words[$m]['words_img']); $words[$m]['words_read1']=''; $words[$m]['words_read2']=''; $words[$m]['words_read3']=''; $words[$m]['words_voice1']=''; $words[$m]['words_voice2']=''; $words[$m]['words_voice3']=''; /* $words[$m]['words_n']=''; $words[$m]['words_pron']=''; $words[$m]['words_adj']=''; $words[$m]['words_num']=''; $words[$m]['words_v']=''; $words[$m]['words_adv']=''; $words[$m]['words_art']=''; $words[$m]['words_prep']=''; $words[$m]['words_conj']=''; $words[$m]['words_int']=''; $words[$m]['words_vt']=''; $words[$m]['words_vi']=''; $words[$m]['words_other']=''; $words[$m]['jz_en1']=''; $words[$m]['jz_cn1']=''; $words[$m]['jz_en2']=''; $words[$m]['jz_cn2']=''; $words[$m]['jz_en3']=''; $words[$m]['jz_cn3']=''; */ } $this->excel($words,$path); } unlink('./export.zip'); exec("zip -r export.zip export/"); echo "over\n"; sleep(3600*24); } } private function excel($words,$path){ ini_set("memory_limit","1024M"); //使用模板 $template = './template/words.xlsx'; //加载excel文件,设置模板 $objPHPExcel = \PHPExcel_IOFactory::load($template); //获得当前sheet对象 $PHPSheet = $objPHPExcel->getActiveSheet(); $fileName = $path."/words.xlsx"; $PHPSheet->setTitle("Sheet1"); //插入数据从第二行开始 $startRow = 2; foreach ($words as $k=>$v) { $sign = 'A'; foreach($v as $m=>$n){ $PHPSheet->setCellValue($sign.$startRow,$n); $sign++; } $startRow++; } //选择写入格式 $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //判断文件是否已存在,存在就删除 if (file_exists($fileName)) { unlink($fileName); } //保存文件 $PHPWriter->save($fileName); } }