开发框架:
thinkphp3.2
Word插件:
PHPWord
一、安装PHPWord
安装PHPWord,可以通过下面2中方式:
1. git地址下载:https://github.com/PHPOffice/PHPWord,下载完成后将文件复制到项目目录下
2. 使用composer安装::composer require phpoffice/phpword
二、在项目中引入PHPWord
2.1 在项目根目录的index.php文件中,加入以下代码,加载PHPWord(phpword在vendor下)
require ‘./vendor/autoload.php’;
2.2 在需要的controller用命名空间的方式引入PHPWord相关类
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TemplateProcessor;
public function word(){
$PHPWord = new PhpWord();
$templateProcessor = new TemplateProcessor('static/abv.docx'); //模板
$templateProcessor->setValue('name','111111'); //模板变量
$templateProcessor->saveAs('static/new.docx');//生成的新文件
}
thinkphp5
<?php
namespace app\index\controller;
use PhpOffice\PhpWord\TemplateProcessor;
class Index
{
public function index()
{
$templateProcessor = new TemplateProcessor('static/abv.docx');
$templateProcessor->setValue('name','111111');
$templateProcessor->saveAs('static/jianding1.docx');
}
}
通过composer安装PHPword后,后台打不开报错( ! ) Fatal error: Cannot declare class ComposerAutoloadComposerStaticInit73f9e72fede2c36621e52f7b610bbb65, because the name is already in use in F:wamp64wwwfastadminvendorcomposerautoload_static.php on line 7 Call Stack
composer回退到 1.x 版本
composer self-update --1