getWords($bach); $destination = ROOT_PATH.'public'.DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx"; $destination_path = DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx"; if(!file_exists($data['dist_fule_file'])){ \exception("报告不存在"); } if(file_exists($destination)){ header("location:".cdnurl($destination_path,true)); }else{ if (copy($data['dist_fule_file'], $destination)) { header("location:".cdnurl($destination_path,true)); } } }catch (Exception $e){ $this->error($e->getMessage()); } } public function preview($bach = null){ header("Content-type:text/html; charset=utf-8");//设置当前页面编码为“utf-8” ini_set("display_errors","On"); error_reporting(E_ALL); $data= $this->getWords($bach); header("location:http://vocs.7in6.com/word.php?doc=".$data['dist_file']); } protected function initData($bach=null){ if (empty($bach)){ $this->error('参数错误','',401); } $where = []; $where['bach'] = $bach; $where['delete_time'] = null; // $where['sample_no'] = array('like','%A%'); $res = Db::name('res')->where($where)->field('entrust_no,name,bach,sell_bach,sample_no,dis,dis_impurity,ben_total,ben,judge,standard_id,create')->find(); if ($res){ $check = Db::name('item_judge_detail')->where('pid',$res['standard_id'])->select(); foreach ($check as $key => $value){ if ($value['params'] == '溶剂残留总量'){ $res['dis_max'] = $value['max']; } if ($value['params'] == '溶剂杂质总量'){ $res['dis_impurity_max'] = $value['max']; } if ($value['params'] == '苯系物总量'){ $res['ben_total_max'] = $value['max']; } if ($value['params'] == '苯含量'){ $res['ben_max'] = $value['max']; } } $res['create'] = date('Y',strtotime($res['create'])).'.'.date('m',strtotime($res['create'])).'.'.date('d',strtotime($res['create'])); if ($res['judge'] == 1){ $res['judge'] = '合格'; }else{ $res['judge'] = '不合格'; } unset($res['standard_id']); return $res; } return null; } /** * @return string */ protected function getWords($bach) { $data=$this->initData($bach); return $this->generateFile($data); } /** * @return string */ protected function generateFile($data) { $data['dist_file']=DS."uploads".DS."words".DS.$data['bach'].".docx"; $data['dist_fule_file']=ROOT_PATH.'public'.DS."uploads".DS."words".DS.$data['bach'].".docx"; if(file_exists($data['dist_fule_file'])){ return $data; } $temp_path=ROOT_PATH.'public'.config("site.ben"); $exten=substr($temp_path, strrpos($temp_path, '.')+1); if($exten!='docx'){ $this->error("模板文件请上传docx格式"); } $templateProcessor=new \PhpOffice\PhpWord\TemplateProcessor($temp_path); $templateProcessor->setValue('entrust_no',$data['entrust_no']);//替换变量name $templateProcessor->setValue('name',$data['name']);//替换变量name $templateProcessor->setValue('bach',$data['bach']);//替换变量name $templateProcessor->setValue('create',$data['create']);//替换变量name $templateProcessor->setValue('dis_max',$data['dis_max']);//替换变量name $templateProcessor->setValue('dis',$data['dis']);//替换变量name $templateProcessor->setValue('dis_impurity_max',$data['dis_impurity_max']);//替换变量name $templateProcessor->setValue('dis_impurity',$data['dis_impurity']);//替换变量name $templateProcessor->setValue('ben_total_max',$data['ben_total_max']);//替换变量name $templateProcessor->setValue('ben_total',$data['ben_total']);//替换变量name $templateProcessor->setValue('ben_max',$data['ben_max']);//替换变量name $templateProcessor->setValue('ben',$data['ben']);//替换变量name $templateProcessor->setValue('judge',$data['judge']);//替换变量name $templateProcessor->saveAs($data['dist_fule_file']);//另存为 return $data; } }