phpword根据模板导出word

参考网址:http://phpword.readthedocs.io/en/latest/installing.html

在composer.json中添加
{
    "require": {
       "phpoffice/phpword": "v0.14.*" } }


然后composer update


public function export()
{
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template\mubanword.docx');//模板word的路径,可以在storage\app下建立template文件夹

$templateProcessor->setValue('name', $name);//给变量赋值

//生成的文档为Word2007
$templateProcessor->saveAs(storage_path('app/public/hello.docx'));//文件保存在这个文件夹中

return response()->download('app/public/hello.docx','重命名.docx');//下载该文件并且重命名
}

注:1、模板word中直接将要填入变量的地方写入${name}即可;

猜你喜欢

转载自www.cnblogs.com/webttt/p/9347386.html