2015年4月20日 星期一

PHPWord 的使用方法整理(一)最基本的使用方式

一、從 GitHub 下載函式庫的壓縮檔( PHPWord-master.zip),下載連結如下:
https://github.com/PHPOffice/PHPWord/archive/master.zip

二、解壓縮後,主要的目錄結構如下
docs/    說明文件
samples/     範例程式
src/    主要的函式庫程式碼
tests/    測試資料夾
將 src/ 下的 PhpWord 這一個目錄,整個複製到你存放函式庫的地方,我用的是 XOOPS,所以我把他放到 xoops_lib/Frameworks/ 下面

三、再來就是一個簡單的產生 Word 案的程式碼

// 因為我用的是 XOOPS 所以要引入 header.php ,才能用一些常數 // 如果你不是用 XOOPS ,下面的這一行就不一定要有 include "header.php"; // 因為 XOOPS 有定義好 XOOPS_PATH 這個路徑常數,如果沒有的話,你得改一下這一行 require_once XOOPS_PATH.'/Frameworks/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); $phpWord = new \PhpOffice\PhpWord\PhpWord(); // 新增一個段落 $section = $phpWord->addSection(); // 在段落裏增加內容 $section->addText(htmlspecialchars('這是一個測試的文字段落')); // 在這裏我要把他直接輸出到瀏覽器裏,讓你可以直接下載,檔名叫 phpword_01.docx header('Content-Type: application/vnd.ms-word'); header('Content-Disposition: attachment;filename="phpword_01.docx"'); header('Cache-Control: max-age=0'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('php://output'); // 如果你要直接生成 word 檔,請將上面這一行改為下面這一行即可 // $objWriter->save('phpword_01.docx'); // 一定要有 exit() exit();





參考資料:
1、PHPWord in GitHub
https://github.com/PHPOffice/PHPWord

2、PHPWord Document
http://phpword.readthedocs.org/en/latest/index.html



1 則留言:

madeirafaciane 提到...

Blackjack, Casino and Table Games - Mapyro
Search Blackjack, Casino and Table Games near Me - See what your friends are 강원도 출장샵 saying about Blackjack, Casino 여수 출장샵 and 여수 출장안마 Table 남원 출장안마 Games near you 광명 출장안마 from Mapyro