步骤:
-
安装 PHPWord(通过 Composer):
1 composer require phpoffice/phpword 2 -
准备一个
.docx模板(把你的.doc另存为.docx格式),并将占位符改为类似$ {activityName}(PHPWord 默认识别$ {xxx})。 -
PHP 代码示例:
<?php require_once 'vendor/autoload.php';use PhpOffice\PhpWord\TemplateProcessor;
// 假设你的模板文件是 activity_template.docx,占位符格式为 {xxx} template = new TemplateProcessor('activity_template.docx');
// 替换数据(根据你表单的实际字段调整)
template->setValue('activityName', '校园歌手大赛'); template->setValue('organizer', '校学生会');
template->setValue('contactPerson', '张三'); template->setValue('phone', '13800138000');
template->setValue('date', '2026年3月15日'); template->setValue('location', '大学生活动中心');
$template->setValue('description', '本次活动旨在丰富校园文化生活...');// 保存到文件
$template->saveAs('活动申请表_已填写.docx');// 或直接输出下载(取消注释下面几行即可)
/*
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="活动申请表_已填写.docx"');
$template->saveAs('php://output');
*/