在項目實際開發或學習中,會遇到把數據導出生成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
效果圖如下:




