模板文件如下:
引入文件
use PhpOffice\PhpWord\TemplateProcessor;
直接上代码
$templateProcessor = new TemplateProcessor($templatePath);
//替换数据
$templateProcessor->setValue('title',$title);
//$info 为列表数据 统计出列表数据的数量
$line = count($info);
//标记 标记表格中第一行第一列的单元格数据
$templateProcessor->cloneRow('name',$line); //name 对应模板中的 ${name}中的name
//for循环遍历,替换模板中的数据
for($i=1;$i<=$line;$i++){
$j = $i-1;
$templateProcessor->setValue('name#'.$i, $info[$j]['name']);//#必不可少
$templateProcessor->setValue('date#'.$i, $info[$j]['date']);
}
//保存 $docPath 文件保存路径
$templateProcessor->saveAs($docPath);
代码中 $templatePath(模板文件路径),$info(列表数据),$docPath(生成文件保存路径)自行定义,查询