PHP实现数据动态写入word模板文件里面

步骤:
  1. 安装 PHPWord(通过 Composer):

    复制代码
    1   composer require phpoffice/phpword
    2   
  2. 准备一个 .docx 模板(把你的 .doc 另存为 .docx 格式),并将占位符改为类似 $ {activityName}(PHPWord 默认识别 $ {xxx})。

  3. 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');
    */

相关推荐
小陈工1 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
H Journey1 小时前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
lly2024066 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨6 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn9996 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏4946 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
功德+n6 小时前
Linux下安装与配置Docker完整详细步骤
linux·运维·服务器·开发语言·docker·centos
明日清晨6 小时前
python扫码登录dy
开发语言·python
我是唐青枫6 小时前
C#.NET gRPC 深入解析:Proto 定义、流式调用与服务间通信取舍
开发语言·c#·.net
JJay.6 小时前
Kotlin 高阶函数学习指南
android·开发语言·kotlin