• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    使用PHPWord生成word文档的方法详解

    本文实例讲述了使用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操作office文档技巧总结(包括word,excel,access,ppt)》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

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

    您可能感兴趣的文章:
    • PHP实现动态创建XML文档的方法
    • PHP读取word文档的方法分析【基于COM组件】
    • php删除txt文件指定行及按行读取txt文档数据的方法
    • PHPExcel导出2003和2007的excel文档功能示例
    • PHP生成word文档的三种实现方式
    • PHP库 查询Mongodb中的文档ID的方法
    • PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
    • PHP sdk文档处理常用代码示例解析
    上一篇:php/JS实现的生成随机密码(验证码)功能示例
    下一篇:php中字符串和整数比较的操作方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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