赞
踩
Composer安装 phpword
composer require phpoffice/phpword
php代码
public function export(){ $id=input('id'); $info=db('record a') ->join('user b','a.user_id=b.id') ->join('school_class c','b.school_class_id=c.id','left') ->join('school_class d','b.school_class_id2=d.id','left') ->join('school_admin e','a.school_admin_id=e.id','left') ->field('a.*,b.nickname,b.username,b.gender,b.age,c.name grade,d.name class,e.name') ->where('a.id',$id)->whereNull('a.deletetime')->find(); if(!$info) $this->error('记录不存在'); //视频对话 $result=db('record_video')->where('record_id',$id)->where('status',2)->whereNull('deletetime')->field('result,createtime')->select(); //测评报告 $user_answer=db('user_answer')->alias('a') ->join('scale b','a.scale_id=b.id') ->where('a.user_id',$info['user_id']) ->whereTime('a.createtime','between',[strtotime($info['day']),strtotime($info['day'])+24*3600-1]) ->whereNull('a.deletetime')->order('a.id desc') ->field('a.content ans_content,b.name ans_name,a.score ans_score')->select(); //历史评语 $record_log=db('record_log')->where('record_id',$id)->select(); //文档模板 $templates = './template/ban.docx'; //声明一个模板对象、读取模板 $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templates); //基础信息 单个设置 $templateProcessor->setValue('nickname',$info['nickname']); $templateProcessor->setValue('gender',$info['gender']==1?'男':'女'); $templateProcessor->setValue('age',$info['age']); $templateProcessor->setValue('grade',$info['grade']); $templateProcessor->setValue('class',$info['class']); $templateProcessor->setValue('name',$info['name']); $templateProcessor->setValue('day',$info['day']); //测评报告列表 复制块 $templateProcessor->cloneBlock('ans_block',count($user_answer),true,false,$user_answer); //咨询全流程记录列表 复制块 foreach ($result as &$item){ $item['ask_day']=date('Y-m-d',$item['createtime']); $ask_result=json_decode($item['result'],true); $html=''; foreach ($ask_result as $v){ $html.='用户'.$v['user'].':'.$v['str'].'<br/>'; } $item['ask_result']=$html; } $templateProcessor->cloneBlock('ask_block',count($result),true,false,$result); //历史评语 复制行 $templateProcessor->cloneRow('com_day',count($record_log)); foreach ($record_log as $k=>$v){ $templateProcessor->setValue('com_day#'.($k+1), date('Y-m-d',$v['createtime'])); $templateProcessor->setValue('com_result#'.($k+1), $v['result']); } $path = $info['nickname'].'_'.$info['day']."_测评报告.docx"; //生成新的word $templateProcessor->saveAs($path); //发送到浏览器 \fast\Http::sendToBrowser($path); }
模板样式
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。