[TOC]
## 安裝
需要使用`phpoffice/phpword`擴展,請自行通過composer安裝
安裝命令:
`composer require phpoffice/phpword`
`composer require smalot/pdfparser`(用于讀取pdf文件,如果你項目不需要讀取pdf格式文件可以不安裝)
## 命名空間
~~~
use woo\common\helper\Word;
~~~
注:wooadmin 3.0.8版本以后支持
## 讀取文件
~~~
/**
* 讀取word、txt文件內容
* @param string $file 文件路徑
* @return array|false|string 一行一個數組元素
* @throws \Exception
*/
public static function read(string $file)
~~~
接口示例:
~~~
$content = Word::read('/uploads/qa/20240105/12250a24e6e7e839b198d5a094c229f1.docx');
~~~
注:只支持.docx .txt文件的讀取,不支持.doc
## 寫入文件
~~~
/**
* 將內容寫入word文件 (只支持寫入.docx文件)
* @param string|array $content 內容 字符串 如果是數組一行一個元素
* @param string $saveName 保存的文件路徑+文件名 如果只是路徑就自動創建文件名
* @param bool|string $downloadName 是否提供下載 可以是字符串為下載時候顯示的文件名
* @return string|\think\response\File
* @throws \Exception
*/
public static function write(string|array $content, string $saveName = '', bool|string $downloadName = false)
~~~
接口示例:
~~~
$content = '測試內容';
return Word::write($content, '', '下載文件名');
~~~
~~~
// 多行
$content = ['測試內容', '測試內容2'];
return Word::write($content, '', '下載文件名');
~~~
~~~
$content = [['測試內容', ['bold' => true, 'name' => '宋體', 'size' => 16]], ['測試內容2', ['underline' => 'single', 'color' => '#ff0000']]];
return Word::write($content, '', '下載文件名');
~~~
* `0`:要添加的文本內容,可以是字符串。
* `1`:可選參數,用于設置文本的字體樣式。可以是一個 `PhpOffice\PhpWord\Style\Font` 對象,或者是一個字體樣式的數組。常用的字體樣式屬性包括:
* `bold`:是否加粗,布爾值(`true` 或 `false`)。
* `italic`:是否斜體,布爾值(`true` 或 `false`)。
* `underline`:是否下劃線。 single
* `color`:文本顏色,可以是顏色名稱或十六進制顏色代碼。
* `size`:字體大小,整數值。
* `name`:字體名稱,字符串。
* `2`:可選參數,用于設置文本所在段落的樣式。可以是一個 `PhpOffice\PhpWord\Style\Paragraph` 對象,或者是一個段落樣式的數組。常用的段落樣式屬性包括:
* `alignment`:對齊方式,可以是 `'left'`、`'right'`、`'center'` 或 `'both'`。
* `indentation`:縮進,可以是整數值或數組,包括 `firstLine`(首行縮進)和 `left`(左縮進)。
* `spaceAfter`:段后間距,整數值。
* `spaceBefore`:段前間距,整數值。
不過測試,有些屬性并沒有效果,對樣式有要求的還是建議"html生成word",或者"模板文件生成"。
## html生成word
~~~
/**
* 根據html模板生成word文件
* @param string $html html模板內容
* @param string $saveName 保存的文件路徑+文件名 如果只是路徑就自動創建文件名
* @param bool|string $downloadName 是否提供下載 可以是字符串為下載時候顯示的文件名
* @return string|\think\response\File
* @throws \Exception
*/
public static function html2word(string $html, string $saveName = '', bool|string $downloadName = false)
~~~
接口示例:
~~~
return Word::html2word(file_get_contents('test.html'), '', '下載文件名');
~~~
~~~
$html = '<body><p>測試內容</p></body>';
return Word::html2word($html, '', '下載文件名');
~~~
自行讀取出html模板內容,或直接傳入寫模板字符串。
html模板示例:
~~~
<html>
<head>
<style>
.bbb{ color: red;}
</style>
</head>
<body>
<p class="bbb">css測試</p>
<p style="text-indent: 24px;font-size:16px;font-family:方正仿宋_GBK;line-height: 3;font-weight: bold;margin: 0;">內容測試</p>
</body>
</html>
~~~
html模板自己找一個路徑存放,然后把詳細地址傳過去。
## word模板生成
~~~
/**
* word模板生成
* @param string $templatePath word模板路徑
* @param array $params 替換的變量列表
* @param string $saveName 保存的文件路徑+文件名 如果只是路徑就自動創建文件名
* @param bool|string $downloadName 是否提供下載 可以是字符串為下載時候顯示的文件名
* @return string|\think\response\File
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
*/
public static function template(string $templatePath, array $params = [], string $saveName = '', bool|string $downloadName = false)
~~~
接口示例:
~~~
// 循環動態替換、外加樣式處理
$q = function () {
$qa = [
[
'你吃飯了沒有1?',
'吃過了。'
],
[
'你吃飯了沒有2?',
'吃過了。'
],
[
'你吃飯了沒有3?',
'吃過了。'
]
];
$textRun = new \PhpOffice\PhpWord\Element\TextRun;
foreach ($qa as $key => $item) {
if ($key != 0) {
$textRun->addTextBreak();// 插入換行符
}
$textRun->addText('問:' . $item[0] . str_repeat(' ', max(48 - mb_strlen($item[0]), 0)), ['size' => 16, 'underline' => 'single', 'bold' => true, 'name' => '方正仿宋_GBK']);
$textRun->addTextBreak();// 插入換行符
$textRun->addText('答:' . $item[1] . str_repeat(' ', max(48 - mb_strlen($item[1]), 0)), ['size' => 16, 'underline' => 'single', 'bold' => true, 'name' => '方正仿宋_GBK']);
}
return $textRun;
};
return Word::template('test.docx', [
'num' => 1,
'year' => 2024,
'month' => 1,
'day' => 8,
'address' => '測試地址',
'name' => "張三\n李四",
'name2' => '李四',
'q' => $q
], '', '下載文件');
~~~
word模板示例:

1.word模板中使用`${變量名}`的格式設置變量;
2.模板中所有的變量都要傳遞過來,否則會被保留;
3.系統內置變量有:
~~~
$sys_params = [
'sys.year' => date('Y'),
'sys.month' => date('m'),
'sys.day' => date('d'),
'sys.hour' => date('H'),
'sys.minute' => date('i'),
'sys.second' => date('s')
];
~~~
4.如果當前請求已登錄,可使用`${login.字段名}`獲取相關用戶信息;
5.獲取總頁數變量未實現,歡迎大家測試告訴我
## word轉換為html內容
~~~
/**
* 將word文件內容轉換為html內容
* @param string $file
* @return false|string
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public static function word2html(string $file)
~~~
接口示例:
~~~
return Word::word2html('bbc.docx');
~~~