使用PHPWord生成word文档的方法详解


Posted in PHP onJune 06, 2019

本文实例讲述了使用PHPWord生成word文档的方法。分享给大家供大家参考,具体如下:

有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑。PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档。

安装

我们使用Composer 来安装PHPWord。

composer require phpoffice/phpword

如何使用

自动加载

安装好phpword后,新建一个php文档,引入autoload.php。

require 'vendor/autoload.php';

实例化

实例化并新增一个空白页。

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();

添加文字内容

向空白页添加文字内容,可以设置文字的样式,包括字体、颜色、字号、粗体等等。

$fontStyle = [
  'name' => 'Microsoft Yahei UI',
  'size' => 20,
  'color' => '#ff6600',
  'bold' => true
];
$textrun = $section->addTextRun();
$textrun->addText('你好,这是生成的Word文档。 ', $fontStyle);

链接

可以为Word文档中的文字添加用于点击跳转的链接。

$section->addLink('https://www.helloweba.net', '欢迎访问Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
$section->addTextBreak();

图片

可以在word中添加图片,如图片地址logo.png,尺寸为64x64。图片源也可以是远程图片。

$section->addImage('logo.png', array('width'=>64, 'height'=>64));

页眉

为Word文档添加页眉。

$header = $section->addHeader();
$header->addText('Subsequent pages in Section 1 will Have this!');

页脚

为word文档添加页脚,页脚内容是页码,格式居中。

$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));

增加一页

继续增加一页,加入内容。

$section = $phpWord->addSection();
$section->addText('新的一页.');

表格

增加一个基础表格,可以设置表格的样式。

$header = array('size' => 16, 'bold' => true);
$rows = 10;
$cols = 5;
$section->addText('Basic table', $header);
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
  $table->addRow();
  for ($c = 1; $c <= 5; $c++) {
    $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
  }
}

生成Word文档

如果你想生成word文档放在服务器上,可以使用:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('hellwoeba.docx');

下载Word文档

如果你想直接下载Word文档,不在服务器上保存的话,可以使用:

$file = 'test.docx';
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');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");

上述代码会强制浏览器下载为word文档。

更多有关PHPWord的内容,请参考PHPWord文档:http://phpword.readthedocs.org/.

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
用PHP制作静态网站的模板框架(二)
Oct 09 PHP
投票管理程序
Oct 09 PHP
php下的权限算法的实现
Apr 28 PHP
php session 错误
May 21 PHP
PHP fgetcsv 定义和用法(附windows与linux下兼容问题)
May 29 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
Jun 24 PHP
php中使用PHPExcel读写excel(xls)文件的方法
Sep 15 PHP
PHP遍历文件夹与文件类及处理类用法实例
Sep 23 PHP
joomla组件开发入门教程
May 04 PHP
laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
Oct 12 PHP
PHP count_chars()函数讲解
Feb 14 PHP
php 实现简单的登录功能示例【基于thinkPHP框架】
Dec 02 PHP
php/JS实现的生成随机密码(验证码)功能示例
Jun 06 #PHP
PHP实现的62进制转10进制,10进制转62进制函数示例
Jun 06 #PHP
PHP cookie,session的使用与用户自动登录功能实现方法分析
Jun 05 #PHP
PHP实现获取url地址中顶级域名的方法示例
Jun 05 #PHP
PHP token验证生成原理实例分析
Jun 05 #PHP
php版本CKEditor 4和CKFinder安装及配置方法图文教程
Jun 05 #PHP
PHP 数组黑名单/白名单实例代码详解
Jun 04 #PHP
You might like
php中将字符串转为HTML的实体引用的一个类
2013/02/03 PHP
解析wamp5下虚拟机配置文档
2013/06/27 PHP
php图片缩放实现方法
2014/02/20 PHP
多个Laravel项目如何共用migrations详解
2018/09/25 PHP
thinkphp5框架实现数据库读取的数据转换成json格式示例
2019/10/10 PHP
nodeType属性返回被选节点的节点类型介绍
2013/11/22 Javascript
JS实现方向键切换输入框焦点的方法
2015/08/19 Javascript
jquery日历插件datepicker用法分析
2016/01/22 Javascript
JQuery 动态生成Table表格实例代码
2016/12/02 Javascript
scroll事件实现监控滚动条并分页显示(zepto.js)
2016/12/18 Javascript
nodejs基于express实现文件上传的方法
2018/03/19 NodeJs
Javascript原型链及instanceof原理详解
2020/05/25 Javascript
Python 深入理解yield
2008/09/06 Python
利用一个简单的例子窥探CPython内核的运行机制
2015/03/30 Python
简单介绍Python中的round()方法
2015/05/15 Python
Python数据操作方法封装类实例
2017/06/23 Python
Python中join函数简单代码示例
2018/01/09 Python
numpy使用技巧之数组过滤实例代码
2018/02/03 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
2018/05/17 Python
Python3使用Matplotlib 绘制精美的数学函数图形
2019/04/11 Python
Django在Model保存前记录日志实例
2020/05/14 Python
python实现密码验证合格程序的思路详解
2020/06/01 Python
Giglio德国网上精品店:奢侈品服装和配件
2016/09/23 全球购物
大都会艺术博物馆商店:The Met Store
2018/06/22 全球购物
表扬信格式
2014/01/12 职场文书
志愿者服务感言
2014/02/27 职场文书
平面设计专业大学生职业规划书
2014/03/12 职场文书
二手房购房意向书范本
2014/04/01 职场文书
绿色家庭事迹材料
2014/05/01 职场文书
中华美德颂演讲稿
2014/05/20 职场文书
社区关爱留守儿童活动方案
2014/08/22 职场文书
见习报告格式要求
2014/11/04 职场文书
环境卫生整治简报
2015/07/20 职场文书
Sql Server之数据类型详解
2022/02/28 SQL Server
vue使用localStorage持久性存储实现评论列表
2022/04/14 Vue.js
windows server2008 开启端口的实现方法
2022/06/25 Servers