美文网首页
hf3.0 实现phpword 根据模板导出word文档

hf3.0 实现phpword 根据模板导出word文档

作者: geeooooz | 来源:发表于2023-07-18 16:13 被阅读0次

引用一些网址:
https://blog.csdn.net/qq_42228630/article/details/124314564
https://www.kancloud.cn/shuiyueju/phpword16/1118709 这个比较重要

安装 PHPWord 库
composer require phpoffice/phpword

编写ExportTask 类

<?php


namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use PhpOffice\PhpWord\TemplateProcessor;

#[Controller(prefix: "lgbhf/ExportTask")]
class ExportTask extends AbstractController
{
    /**
     * User: wujiawei
     * DateTime: 2023/7/19 16:05
     * describe:模板替换
     * @throws \PhpOffice\PhpWord\Exception\CopyFileException
     * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
     */
    #[GetMapping(path: "index")]
    public function index()
    {
        // 模板文件路径
        $templateFile = BASE_PATH.'/public/excel/temp.docx';
        // 创建 TemplateProcessor 实例
        $templateProcessor = new TemplateProcessor($templateFile);
        // 替换变量
        $templateProcessor->setValue('time', date('Y-m-d',time()));
        $templateProcessor->setValue('danwei', '河北七海');
        $templateProcessor->setValue('baifangren', '张大大');
        $templateProcessor->setValue('zhuti', '谈合作');
        // 图片数据数组,包含多个图片的路径
        $imagePaths = [
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg',
            'http://lgbv3.linggongbang.cn/Upload/V3/head/2023-07-19/64b7357469a42.jpg'
            // 其他图片路径...
        ];
        // 替换变量为图片
        for ($i=0;$i < 8;$i++){
            $img = $imagePaths[$i] ?? null;
            if($img){
                $templateProcessor->setImageValue("img$i", array('path' => $img, 'width' => 150, 'height' => 150));
            }else{
                $templateProcessor->setValue("img$i",'');
            }
            unset($img);
        }
        // 保存生成的 Word 文档
        $generatedFile = BASE_PATH.'/public/excel/generated.docx';
        $templateProcessor->saveAs($generatedFile);
    }


    #[GetMapping(path: "createCloneBlock")]
    public function createCloneBlock(){
        //拿到所有的参数
        $data = array(
            array('customer_name' => 'John', 'customer_address' => 25),
            array('customer_name' => 'Emily', 'customer_address' => 30),
            array('customer_name' => 'Michael', 'customer_address' => 35)
        );
        $num = count($data);//参数数量

        // 模板文件路径
        $templateFile = BASE_PATH.'/public/excel/temp2.docx';
        // 创建 TemplateProcessor 实例
        $templateProcessor = new TemplateProcessor($templateFile);
        // 复制块
        $templateProcessor->cloneBlock('block_name',$num,true,true);

        // 在循环中复制块,并为每个复制的块赋予不同的值
        for ($i=1;$i<=$num;$i++){
            $templateProcessor->setValue("customer_name#$i", $data[$i-1]['customer_name']);
            $templateProcessor->setValue("customer_address#$i", $data[$i-1]['customer_address']);
        }
        // 保存生成的 Word 文档
        $generatedFile = BASE_PATH."/public/excel/".time()."aaa.docx";
        $templateProcessor->saveAs($generatedFile);
        return 'ok';
    }
}

第一个模板是这样的:


image.png

导出的是这样的:


image.png

第二个模板主要是实现 块 的复制
模板是这样的:


image.png

最后导出是这样的:


image.png

相关文章

网友评论

      本文标题:hf3.0 实现phpword 根据模板导出word文档

      本文链接:https://www.haomeiwen.com/subject/idwnudtx.html