首先,使用composer安装PHPWord
composer require phpoffice/phpword
使用PhpWord读取word模板并替换内容
- 打开一个docx模板,在里面设置占位符:${占位符}
![](https://i-blog.csdnimg.cn/blog_migrate/12d5ff94c02d48e26f0a42d833ed4588.png)
- 然后,编写代码实现Word文档内容的替换:
<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use Exception;
use PhpOffice\PhpWord\TemplateProcessor;
class Test extends Controller
{
protected $request;
public function _initialize()
{
$this->request = Request::instance();
}
/**
* @api {get} /api/test/test 测试
* @apiName test
* @apiGroup Test
*/
public function test()
{
try {
// 模板位置
$docxPath = ROOT_PATH . 'public' . DS . 'file' . DS . '8.docx';
/