最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

简单的几句PHP代码实现生成word文档功能

来源:http://www.erdangjiade.com/php/8569.html 沐浴春风 2018-09-03 15:45浏览(887)

以前用PHP实现转化为Word很难,现在这个问题解决了。很多时候我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑。PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档;

0、请不要问“在不在”之类的问题,有问题直接问!1、学生或暂时没有工作的童鞋,整站资源免费下载!2、¥9.9充值终身VIP会员,加我微信,826096331 拉你进VIP群学习!3、程序员加油,技术改变世界。 在线 充值

简单的几句PHP代码实现生成word文档功能
分类:PHP > 函数 难易:中级
查看演示 下载资源: 下载资源 下载积分: 50 积分

安装 我们使用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文档。
声明:本文为原创文章,如需转载,请注明来源erdangjiade.com并保留原文链接:https://www.erdangjiade.com/php/8569.html
评论7
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

  • 头像 6楼
    09-18 17:19
    xiufui
    很强大 以后有用在下<br />
  • 头像 5楼
    08-27 18:39
    金木_一花
    一直评论一直爽
  • 头像 4楼
    01-15 09:07
    jeffxie
    很强大 以后有用在下
  • 头像 3楼
    12-10 09:23
    DawnZhao
    感觉不是很强大!
  • 头像 板凳
    11-18 22:54
    hsxy8
    怎么没效果啊
  • 头像 椅子
    11-07 13:51
    mengkai
    已经下载
  • 头像 沙发
    10-18 16:10
    hhott
    很不错的源码.
1 2