安装->调用案例
composer require phpoffice/phpword
public function exportRecords($model){
$model->name= '感触';
$model->gender = 1;//单选
$model->symptom = [2,4];//多选
$model->avatar = 'https://www.baidu.com/img/baidu_jgylogo3.gif';
$gender = [1=>'男',2=>'女',3=>'未设置'];
$symptomName = [0=>'无症状',1=>'头痛',2=>'头晕',3=>'心悸',4=>'胸闷',5=>'胸痛'];
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('4138.docx');//导入文件在laravel/public下
$templateProcessor->setValue('name', $model->name);
$templateProcessor->setValue('gender', $gender[$model->gender]);
$templateProcessor->setImageValue('picture', ['path' =>$model->avatar,'width'=>170,'height'=>220]);
$templateProcessor->setValue('symptom',implode(',',array_intersect_key($symptomName,array_flip($model->symptom))));
$templateProcessor->saveAs('病历.docx');//导出文件在laravel/public下
return response()->download('病历.docx' );
}
Word模板部分截图如下,模板的代码需要在编辑器输好再复制过去