在thinkPHP3.2版本中应用

下载

官网下载phpWord插件:
GitHub

安装

但是从github官网直接下载的phpword文件包会因为缺少autoload.php文件,导致类自动加载失败
需要用composer下载:
Windows下载安装composer之后,
进入项目根目录,安装phpword

  1. composer install phpoffice/phpword

安装之后,根目录下会有一个vendor文件夹

引用

在使用的地方引入:
image.png

备注

使用类名的时候前面加上一个’\’;

  1. require 'vendor/autoload.php';
  2. $PHPWord = new \PhpOffice\PhpWord\PhpWord();

在使用\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html)插入带有html标签内容的时候,打开Word会一直无法打开,提示xml pasring error
原因是导出时部分特殊字符转义问题
查阅相关的xml转义字符,发现在xml文件中 “&”、“<”、“>”、单引号、双引号会引起xml文件编译错误,特别是“&”和“<”是“坚决”不能在xml文件中出现的。

**
编写某些格式的文档,尤其是基于 XML 的文档,需要正确的输出转义。如果没有它,当您在其中放入特殊字符(如与号、引号和其他字符)时,您的文档可能会损坏。
可以通过两种方式执行转义:软件开发人员在库外和通过内置机制在库内进行转义。默认情况下,为了与 v0.13.0 之前的版本向后兼容,内置机制被禁用。要在 PHPWord 配置文件中打开setoutputEscapingEnabled选项true或在运行时使用以下指令:

  1. // 对word文档做任何事情之前添加以下代码:
  2. \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
  3. // 这将自动转义任何有问题的字符。

使用

demo:

  1. // 创建一个phpword对象
  2. $PHPWord = new \PhpOffice\PhpWord\PhpWord();
  3. $PHPWordHelper= new \PhpOffice\PhpWord\Shared\Font();
  4. $PHPWord->setDefaultFontName('仿宋'); // 全局字体
  5. $PHPWord->setDefaultFontSize(16); // 全局字号为3号
  6. // 设置文档的属性,这些在对文档右击属性可以看到,也可以省去这些步骤
  7. $properties = $PHPWord->getDocInfo();
  8. $properties->setCreator('张三'); // 创建者
  9. $properties->setCompany('某公司'); // 公司
  10. $properties->setTitle('某某文档'); // 标题
  11. $properties->setDescription('http://wangye.org'); // 描述
  12. $properties->setLastModifiedBy('李四'); // 最后修改
  13. $properties->setCreated( time() ); // 创建时间
  14. $properties->setModified( time() ); // 修改时间
  15. $properties->setSubject('My subject');
  16. $properties->setKeywords('my, key, word');
  17. // 添加3号仿宋字体到'FangSong16pt'留着下面使用
  18. $PHPWord->addFontStyle('FangSong16pt', array('name'=>'仿宋', 'size'=>16));
  19. // 添加段落样式到'Normal'以备下面使用
  20. $PHPWord->addParagraphStyle(
  21. 'Normal',array(
  22. 'align'=>'both',
  23. 'spaceBefore' => 0,
  24. 'spaceAfter' => 0,
  25. 'spacing'=>$PHPWordHelper->pointSizeToTwips(2.8),
  26. 'lineHeight' => 1.19, // 行间距
  27. 'indentation' => array( // 首行缩进
  28. 'firstLine' => $PHPWordHelper->pointSizeToTwips(32)
  29. )
  30. )
  31. );
  32. // Section样式:上3.5厘米、下3.8厘米、左3厘米、右3厘米,页脚3厘米
  33. // 注意这里厘米(centimeter)要转换为twips单位
  34. $sectionStyle = array(
  35. 'orientation' => null,
  36. 'marginLeft' => $PHPWordHelper->centimeterSizeToTwips(3),
  37. 'marginRight' => $PHPWordHelper->centimeterSizeToTwips(3),
  38. 'marginTop' => $PHPWordHelper->centimeterSizeToTwips(3.5),
  39. 'marginBottom' => $PHPWordHelper->centimeterSizeToTwips(3.8),
  40. 'pageNumberingStart' => 1, // 页码从1开始
  41. 'footerHeight' => $PHPWordHelper->centimeterSizeToTwips(3),
  42. );
  43. $section = $PHPWord->addSection($sectionStyle); // 添加一节
  44. // 下面这句是输入文档内容,注意这里用到了刚才我们添加的
  45. // 字体样式FangSong16pt和段落样式Normal
  46. $section->addText('文档内容', 'FangSong16pt', 'Normal');
  47. $section->addTextBreak(1); // 新起一个空白段落
  48. $content = "<h3 style='font-weight: bold;'>$title</h3><br/>";
  49. $content .= "<h5 style='font-weight: bold;'>提交人:$username</h5><br/>";
  50. $content .= "<h5 style='font-weight: bold;'>漏洞时间:$time</h5><br/>";
  51. $content .= "<h5 style='font-weight: bold;'>漏洞等级:$rank_text</h5><br/>";
  52. $content .= "<h5 style='font-weight: bold;'>漏洞类别:$category</h5><br/>";
  53. $content .= "<h5 style='font-weight: bold;'>漏洞状态:$type_text</h5><br/>";
  54. $content .= "<h5 style='font-weight: bold;'>漏洞URL:$domain</h5><br/>";
  55. $content .= "<hr />";
  56. $content .= "<h4 style='font-weight: bold;'>漏洞内容:</h4>";
  57. \PhpOffice\PhpWord\Shared\Html::addHtml($section, $content); // 插入html内容
  58. $section->addTextBreak(1);
  59. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
  60. $filename = 'demo.docx';
  61. ob_end_clean();//清除缓冲区,避免乱码
  62. Header("Content-type: application/octet-stream");
  63. Header("Accept-Ranges: bytes");
  64. Header("Accept-Length:" . filesize($filename));
  65. Header("Content-Disposition: attachment;filename=" . $filename);
  66. $objWriter->save('php://output'); //生成Word并下载

常用配置及操作

0.18.2官网详细