[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模板示例: ![](https://img.kancloud.cn/72/f1/72f1b87c15cb3bb6ed8fa45fba617ee7_2104x1304.png) 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'); ~~~