在项目实际开发或学习中,会遇到把数据导出生成word文档的需求。这里就用优雅、流行的laravel框架,来简单的教大家实现。
phpword,它可以很方便的实现word文档的生成,同时可以在word中添加表格、目录、图片、超链接、页眉、页脚等功能强大。
安装phpWord
要求
强制性:
PHP 5.3.3+
- XML解析器扩展
- Zend \ Escaper组件
- Zend \ Stdlib组件
- Zend \ Validator组件
可选的:
-邮编扩展
- GD扩展
- XMLWriter扩展
- XSL扩展
- dompdf库
PHPWord通过Composer安装。你只需要在你的包中添加对PHPWord的依赖。
{ “require” : { “phpoffice / phpword” : “v0.14。*??” } }
添加后执行
composer install
另外,还可以通过命令行安装,如
composer require phpoffice/phpword
示例代码
1 $phpWord = new \PhpOffice\PhpWord\PhpWord(); 2 //设置默认样式 3 $phpWord->setDefaultFontName(‘仿宋‘);//字体 4 $phpWord->setDefaultFontSize(16);//字号 5 6 //添加页面 7 $section = $phpWord->createSection(); 8 9 //添加目录 10 $styleTOC = [‘tabLeader‘ => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT]; 11 $styleFont = [‘spaceAfter‘ => 60, ‘name‘ => ‘Tahoma‘, ‘size‘ => 12]; 12 $section->addTOC($styleFont, $styleTOC); 13 14 //默认样式 15 $section->addText(‘Hello PHP!‘); 16 $section->addTextBreak();//换行符 17 18 //指定的样式 19 $section->addText( 20 ‘Hello world!‘, 21 [ 22 ‘name‘ => ‘宋体‘, 23 ‘size‘ => 16, 24 ‘bold‘ => true, 25 ] 26 ); 27 $section->addTextBreak(5);//多个换行符 28 29 //自定义样式 30 $myStyle = ‘myStyle‘; 31 $phpWord->addFontStyle( 32 $myStyle, 33 [ 34 ‘name‘ => ‘Verdana‘, 35 ‘size‘ => 12, 36 ‘color‘ => ‘1BFF32‘, 37 ‘bold‘ => true, 38 ‘spaceAfter‘ => 20, 39 ] 40 ); 41 $section->addText(‘Hello Laravel!‘, $myStyle); 42 $section->addText(‘Hello Vue.js!‘, $myStyle); 43 $section->addPageBreak();//分页符 44 45 //添加文本资源 46 $textrun = $section->createTextRun(); 47 $textrun->addText(‘加粗‘, [‘bold‘ => true]); 48 $section->addTextBreak();//换行符 49 $textrun->addText(‘倾斜‘, [‘italic‘ => true]); 50 $section->addTextBreak();//换行符 51 $textrun->addText(‘字体颜色‘, [‘color‘ => ‘AACC00‘]); 52 53 //超链接 54 $linkStyle = [‘color‘ => ‘0000FF‘, ‘underline‘ => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE]; 55 $phpWord->addLinkStyle(‘myLinkStyle‘, $linkStyle); 56 $section->addLink(‘http://www.baidu.com‘, ‘百度一下‘, ‘myLinkStyle‘); 57 $section->addLink(‘http://www.baidu.com‘, null, ‘myLinkStyle‘); 58 59 //添加图片 60 $imageStyle = [‘width‘ => 480, ‘height‘ => 640, ‘align‘ => ‘center‘]; 61 $section->addImage(‘./img/t1.jpg‘, $imageStyle); 62 $section->addImage(‘./img/t2.jpg‘,$imageStyle); 63 64 //添加标题 65 $phpWord->addTitleStyle(1, [‘bold‘ => true, ‘color‘ => ‘1BFF32‘, ‘size‘ => 38, ‘name‘ => ‘Verdana‘]); 66 $section->addTitle(‘标题1‘, 1); 67 $section->addTitle(‘标题2‘, 1); 68 $section->addTitle(‘标题3‘, 1); 69 70 //添加表格 71 $styleTable = [ 72 ‘borderColor‘ => ‘006699‘, 73 ‘borderSize‘ => 6, 74 ‘cellMargin‘ => 50, 75 ]; 76 $styleFirstRow = [‘bgColor‘ => ‘66BBFF‘];//第一行样式 77 $phpWord->addTableStyle(‘myTable‘, $styleTable, $styleFirstRow); 78 79 $table = $section->addTable(‘myTable‘); 80 $table->addRow(400);//行高400 81 $table->addCell(2000)->addText(‘学号‘); 82 $table->addCell(2000)->addText(‘姓名‘); 83 $table->addCell(2000)->addText(‘专业‘); 84 $table->addRow(400);//行高400 85 $table->addCell(2000)->addText(‘2015123‘); 86 $table->addCell(2000)->addText(‘小明‘); 87 $table->addCell(2000)->addText(‘计算机科学与技术‘); 88 $table->addRow(400);//行高400 89 $table->addCell(2000)->addText(‘2016789‘); 90 $table->addCell(2000)->addText(‘小傻‘); 91 $table->addCell(2000)->addText(‘教育学技术‘); 92 93 //页眉与页脚 94 $header = $section->createHeader(); 95 $footer = $section->createFooter(); 96 $header->addPreserveText(‘页眉‘); 97 $footer->addPreserveText(‘页脚 - 页数 {PAGE} - {NUMPAGES}.‘); 98 99 //生成的文档为Word2007 100 $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, ‘Word2007‘); 101 $writer->save(‘./word/hello.docx‘); 102 103 //将文档保存为ODT文件... 104 $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, ‘ODText‘); 105 $writer->save(‘./word/hello.odt‘); 106 107 //将文档保存为HTML文件... 108 $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, ‘HTML‘); 109 $writer->save(‘./word/hello.html‘);
注意: Word转HTML时,Word 文档载入的目录写的是相对路径,写绝对路径
时会报错,这个我没有解决,如有方法请留言
参考文档地址:http://phpword.readthedocs.io/en/latest/intro.html
效果图如下: