直接上项目地址吧:
https://github.com/PHPOffice/PHPWord#getting-started

Installation(安装)

PHPWord is installed via Composer. To add a dependency to PHPWord in your project, either

Run the following to use the latest stable version(第一种安装方法)

  1. composer require phpoffice/phpword

or if you want the latest master version(第二种安装方法)

  1. composer require phpoffice/phpword:dev-master

You can of course also manually edit your composer.json file(第三种安装方法)

  1. {
  2. "require": {
  3. "phpoffice/phpword": "v0.14.*"
  4. }
  5. }

Getting started(使用,一句话就搞定)

  1. <?php
  2. require_once './vendor/autoload.php';
  3. use PhpOffice\PhpWord\TemplateProcessor;
  4. $templateProcessor = new TemplateProcessor('./Template.docx');
  5. $templateProcessor->setValue('this_is_player', "小牛逼");
  6. $templateProcessor->saveAs('./helloWorld.docx');

模板如下(Template.docx),红框中的文本就是要替换的。
模板

分类: web

标签:   php   word