https://www.runoob.com/w3cnote/composer-install-and-usage.html
2.安装phpword
cmd命令行:composer require phpoffice/phpword
切记:cmd命令要切换到项目的根目录下
3.测试使用
1)需要word文档:a.docx 内容:
说明人:${user}
电话:${email}
图片:${picture}
2)php代码
include_once "vendor/autoload.php";
$tmp=new \PhpOffice\PhpWord\TemplateProcessor('a.docx');//打开模板
$tmp->setValue('user','李四');//替换变量
$tmp->setValue('email','18888888888');//替换变量
$tmp->setImageValue('picture', ['path' => '1.png','width'=>500,'height'=>500]);//添加图片
$tmp->saveAs('b.docx');//另存为
运行即可。
整个项目目录
切记:一定要切换到项目目录下安装
其他:如果不想安装Composer和phpword,可以使用我的整个源代码,解压后运行即可。下载链接点这里
1.安装Composerhttps://www.runoob.com/w3cnote/composer-install-and-usage.html2.安装phpwordcmd命令行:composer require phpoffice/phpword3.测试使用
用phpword处理docx模板时候始终发生神奇的BUG,就是复制原版例子里的${Value1}进自己的模板然后替换是没问题的,但是只要一改动这个变量文字,PHP做相应替换就失效了。
用了下残废百度无果,一怒翻起google,准确度高多了。
copy一篇比较nice的博文做个备份
转载自:http://wangye.org/blog/archives/943/
==============...
一个
PHP小白的苦逼之路,
PHP接口大家想必只要有点其他语言的基础,都能很快上手,并且能够将数据成功返回给前端。但是只会写接口的程序员不是好程序员,但是
PHP的后端小白除了写接口还能干嘛呢?
正好公司要求
生成word形式的合同,而且是以
模板的形式。这下子就不能用纯生的
PHP去写了,网上也有很多纯生
PHP去写,但是流程很多,代码也很多,小白的我肯定是能偷懒则偷懒,找到了一个通用的三方库(用composer自动导入
PHPoffice/
phpword),引用很方便,就想着po出来给和我一样的小白去偷懒。
To generate a Microsoft Word document using PHP, you can use the PHPWord library, which provides a simple and easy-to-use API for creating and manipulating Word documents in PHP.
Here's an example of how to create a basic Word document using PHPWord:
1. First, download and install the PHPWord library by running the following command in your terminal:
composer require phpoffice/phpword
2. Next, create a new PHP script and include the PHPWord autoload file at the top of your script:
require_once 'vendor/autoload.php';
3. Create a new instance of the `PhpOffice\PhpWord\PhpWord` class:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
4. Add a new section to the document:
$section = $phpWord->addSection();
5. Add some text to the section:
$section->addText('Hello, world!');
6. Finally, save the document to a file:
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('hello_world.docx');
This will create a new Word document named `hello_world.docx` in the current directory, containing the text "Hello, world!" in the first section.
You can customize the document further by adding more sections, paragraphs, tables, images, and other elements using the PHPWord API. For more information and examples, see the PHPWord documentation.
Java2018eclipse:
非常简单:一二三,注意观察各种情况的区别就行了
黄焖鸡要特辣: