经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
tp5使用PHPWord(下载引入/composer两种方式)
来源:cnblogs  作者:夜色0510  时间:2020/12/14 17:28:05  对本文有异议

PHPWORD使用文档

一:引入

tp5.0tp5.1

1composer方式(推荐)

a:根目录下执行:composer require phpoffice/phpword

b:引入:

use PhpOffice\PhpWord\PhpWord;

2:下载引入方式

a:下载PHPWord

地址:https://pan.baidu.com/s/19UctPmT5tdn0SqrEgM56MA

提取码:zxcv

b:放到项目根目录extend文件夹下,目录结构如下:

  

c:引入

use PhpOffice\PhpWord\PhpWord;

二:导出

$file = '../extend/files/pdf.docx';//路径,可更改

$PHPWord = new PhpWord();

$template = $PHPWord->loadTemplate($file);//加载模板

$template->setValue('title', '标题');//替换值

$file = date('Y-m-d-H-i-s') . '.docx';//文件名

$encoded_filename = urlencode($file); // 将文件名进行urlencode转码

$file = str_replace('+', '%20', $encoded_filename);

header("Content-Description: File Transfer");

header('Content-Disposition: attachment; filename="' . $file . '"');

header('Content-Type:application/vnd.openxmlformats-officedocument.wordprocessingml.document');

header('Content-Transfer-Encoding: binary');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Expires: 0');

$template->saveAs('php://output');

格式:

//替换值(模板内为${title},如模板图)

$template->setValue('title', '标题');

 

//选择框(模板内为check0check1<字体是【Wingdings 2>,替换时R是选中,是未选,如模板图)
$template->setValue('check0', 1? 'R' : '£');//与模板内check0对应,变量命名可更改,与模板一致即可

$template->setValue('check1', 0? 'R' : '£');//与模板内check1对应,变量命名可更改,与模板一致即可

 

//复制行

$template->cloneRow('本行最左边的变量名', '要复制的行数');

//复制行-举例(如模板图)

$user = [['no'=>'1', 'name'=>'张三', 'sex'=>''], ['no'=>'2', 'name'=>'李四', 'sex'=>'']];

$rows = count($user);

$template->cloneRow('no', $rows);//复制行no是要复制行的最左边变量,$rows代表复制几行,复制后会是no#1name#1sex#1no#2name#2sex#2这样的

for ($i = 0; $i < $rows; $i++) {

$template->setValue('no#' . ($i + 1), $user[$i]['no']);

$template->setValue('name#' . ($i + 1), $user[$i]['name']);

$template->setValue('sex#' . ($i + 1), $user[$i]['sex']);

}

 

//复制块,也可用于是否显示

$template->cloneBlock('块标签名','数量');//模板内为${块标签名}${/块标签名}html标签一样,成对出现,内容放中间

//复制块-举例(如模板图)

$show_name="显示";

$template->cloneBlock('show',2);//复制两个

$template->setValue('show_name',$show_name);//设置值

$template->cloneBlock('hide',0);//复制0个,代表隐藏,值也不用设了

 

//插入图片(模板内为${img}

$template->setImageValue('img', ['path' => '路径','width'=>500,'height'=>500]);

 

 

模板图

  

 

结果图

 

原文链接:http://www.cnblogs.com/waft/p/14107809.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号