composer require "phpoffice/phpword"
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
模板准备,在需要替换的位置放上${none}用于代码替换
注意:WPS保存的模板WPS和office都能用,office保存的模板只有自己能用!
$filenamedoc = 'static/upload/模板文件.docx'; //模板路径
$phpWord = new PhpWord();
$list = getdata(); //数据
$template = $phpWord->loadTemplate($filenamedoc); //初始化模板
$template->setValue('name', $name); //模板赋值
$finname = md5(uniqid(mt_rand(), true)).'.docx';
$filename = 'static/word' . '/'.$finname;
$template->saveAs($filename); //保存文件