You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP操作MS Word内容控件实现模板数据填充?

实现PHP填充Word内容控件的可行方案

方法一:直接操作DOCX的XML结构

DOCX本质是ZIP压缩包,核心内容存放在word/document.xml中。内容控件在XML里以<w:sdt>节点存在,每个控件对应<w:tag>属性(即你在Word中给内容控件设置的标签名),可通过定位该节点替换内容。

步骤与示例代码:

  1. 解压DOCX模板到临时目录
  2. 读取并修改XML内容
  3. 重新打包为DOCX文件
// 1. 解压模板
$zip = new ZipArchive();
$templatePath = 'template.docx';
$tempDir = sys_get_temp_dir() . '/docx_temp';
mkdir($tempDir);

if ($zip->open($templatePath) === TRUE) {
    $zip->extractTo($tempDir);
    $zip->close();
}

// 2. 用DOM+XPath修改XML(比正则更可靠)
$xmlPath = $tempDir . '/word/document.xml';
$dom = new DOMDocument();
$dom->load($xmlPath);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');

// 从MySQL获取数据(示例)
$customerName = '张三'; // 替换为实际查询结果

// 定位标签为customer_name的内容控件
$nodes = $xpath->query('//w:sdt[w:tag/@w:val="customer_name"]/w:sdtContent');
if ($nodes->length > 0) {
    $contentNode = $nodes->item(0);
    // 清空原有内容
    while ($contentNode->firstChild) {
        $contentNode->removeChild($contentNode->firstChild);
    }
    // 插入新文本节点
    $text = $dom->createElement('w:t', $customerName);
    $run = $dom->createElement('w:r');
    $run->appendChild($text);
    $para = $dom->createElement('w:p');
    $para->appendChild($run);
    $contentNode->appendChild($para);
}

$dom->save($xmlPath);

// 3. 重新打包成DOCX
$newZip = new ZipArchive();
$outputPath = 'filled_document.docx';
if ($newZip->open($outputPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempDir), RecursiveIteratorIterator::LEAVES_ONLY);
    foreach ($files as $name => $file) {
        if (!$file->isDir()) {
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($tempDir) + 1);
            $newZip->addFile($filePath, $relativePath);
        }
    }
    $newZip->close();
}

// 清理临时文件
array_map('unlink', glob("$tempDir/*"));
rmdir($tempDir);

方法二:使用phpdocx库

phpdocx提供了封装好的内容控件替换API,无需手动处理XML结构,适合复杂模板场景:

require_once 'phpdocx/classes/CreateDocx.php';

$docx = new CreateDocxFromTemplate('template.docx');
// 直接根据标签名替换内容控件值
$docx->replaceContentControl(array('customer_name' => $customerName));
$docx->createDocx('filled_document');

该库分开源版和商业版,商业版支持更多复杂控件类型(如表格、复选框等)。

方法三:PHPWord扩展处理

PHPWord原生不支持内容控件直接替换,但可通过遍历文档元素、定位控件节点实现修改,不过该方法复杂度较高,不如前两种方案高效,仅推荐对PHPWord有深度定制需求的场景。

内容的提问来源于stack exchange,提问作者JJ279

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:26:27