在thinkPHP3.2版本中应用
下载
官网下载phpWord插件:
GitHub
安装
但是从github官网直接下载的phpword文件包会因为缺少autoload.php文件,导致类自动加载失败
需要用composer下载:
Windows下载安装composer之后,
进入项目根目录,安装phpword
composer install phpoffice/phpword
安装之后,根目录下会有一个vendor文件夹
引用
在使用的地方引入:
备注
使用类名的时候前面加上一个’\’;
require 'vendor/autoload.php';$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或在运行时使用以下指令:
// 对word文档做任何事情之前添加以下代码:\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);// 这将自动转义任何有问题的字符。
使用
demo:
// 创建一个phpword对象$PHPWord = new \PhpOffice\PhpWord\PhpWord();$PHPWordHelper= new \PhpOffice\PhpWord\Shared\Font();$PHPWord->setDefaultFontName('仿宋'); // 全局字体$PHPWord->setDefaultFontSize(16); // 全局字号为3号// 设置文档的属性,这些在对文档右击属性可以看到,也可以省去这些步骤$properties = $PHPWord->getDocInfo();$properties->setCreator('张三'); // 创建者$properties->setCompany('某公司'); // 公司$properties->setTitle('某某文档'); // 标题$properties->setDescription('http://wangye.org'); // 描述$properties->setLastModifiedBy('李四'); // 最后修改$properties->setCreated( time() ); // 创建时间$properties->setModified( time() ); // 修改时间$properties->setSubject('My subject');$properties->setKeywords('my, key, word');// 添加3号仿宋字体到'FangSong16pt'留着下面使用$PHPWord->addFontStyle('FangSong16pt', array('name'=>'仿宋', 'size'=>16));// 添加段落样式到'Normal'以备下面使用$PHPWord->addParagraphStyle('Normal',array('align'=>'both','spaceBefore' => 0,'spaceAfter' => 0,'spacing'=>$PHPWordHelper->pointSizeToTwips(2.8),'lineHeight' => 1.19, // 行间距'indentation' => array( // 首行缩进'firstLine' => $PHPWordHelper->pointSizeToTwips(32))));// Section样式:上3.5厘米、下3.8厘米、左3厘米、右3厘米,页脚3厘米// 注意这里厘米(centimeter)要转换为twips单位$sectionStyle = array('orientation' => null,'marginLeft' => $PHPWordHelper->centimeterSizeToTwips(3),'marginRight' => $PHPWordHelper->centimeterSizeToTwips(3),'marginTop' => $PHPWordHelper->centimeterSizeToTwips(3.5),'marginBottom' => $PHPWordHelper->centimeterSizeToTwips(3.8),'pageNumberingStart' => 1, // 页码从1开始'footerHeight' => $PHPWordHelper->centimeterSizeToTwips(3),);$section = $PHPWord->addSection($sectionStyle); // 添加一节// 下面这句是输入文档内容,注意这里用到了刚才我们添加的// 字体样式FangSong16pt和段落样式Normal$section->addText('文档内容', 'FangSong16pt', 'Normal');$section->addTextBreak(1); // 新起一个空白段落$content = "<h3 style='font-weight: bold;'>$title</h3><br/>";$content .= "<h5 style='font-weight: bold;'>提交人:$username</h5><br/>";$content .= "<h5 style='font-weight: bold;'>漏洞时间:$time</h5><br/>";$content .= "<h5 style='font-weight: bold;'>漏洞等级:$rank_text</h5><br/>";$content .= "<h5 style='font-weight: bold;'>漏洞类别:$category</h5><br/>";$content .= "<h5 style='font-weight: bold;'>漏洞状态:$type_text</h5><br/>";$content .= "<h5 style='font-weight: bold;'>漏洞URL:$domain</h5><br/>";$content .= "<hr />";$content .= "<h4 style='font-weight: bold;'>漏洞内容:</h4>";\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content); // 插入html内容$section->addTextBreak(1);$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');$filename = 'demo.docx';ob_end_clean();//清除缓冲区,避免乱码Header("Content-type: application/octet-stream");Header("Accept-Ranges: bytes");Header("Accept-Length:" . filesize($filename));Header("Content-Disposition: attachment;filename=" . $filename);$objWriter->save('php://output'); //生成Word并下载
