zoukankan      html  css  js  c++  java
  • 使用PHPWord生成word文档

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

    安装

    我们使用Composer来安装PHPWord。

    composer require phpoffice/phpword

    如何使用

    自动加载

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

    
    
    require 'vendor/autoload.php';
    实例化

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

    
    
    $phpWord = new PhpOfficePhpWordPhpWord();
    
    $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' => PhpOfficePhpWordStyleFont::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' => PhpOfficePhpWordSimpleTypeJc::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 = PhpOfficePhpWordIOFactory::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 = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
    $xmlWriter->save("php://output");
    
    
    
    

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

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

  • 相关阅读:
    Visual Studio: 一键卸载所有组件工具,彻底卸载干净。
    由于未能创建Visual C# 2015编译器,因此未能打开项目xxx。请重新安装Visual Studio。
    MySQL:ROWNUM的假实现
    mysql、MS SQL关于分页的sql查询语句 limit 和row_number() OVER函数
    Redis:默认配置文件redis.conf详解
    Redis:五种数据类型的简单增删改查
    使用控制台对Redis执行增删改查命令
    Redis:高性能文件缓存key-value储存
    redis : 桌面管理工具 redis-desktop-manager使用指南
    SqlServer :利用快捷键快速查看 字段说明查询及表结构 (小技巧)
  • 原文地址:https://www.cnblogs.com/cqlb/p/9336904.html
Copyright © 2011-2022 走看看