感触life

Laravel+phpword导出word

项目地址:https://github.com/PHPOffice/PHPWord#getting-started

安装->调用案例

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模板部分截图如下,模板的代码需要在编辑器输好再复制过去

码字很辛苦,转载请注明来自感触life-博客《Laravel+phpword导出word》

评论