dcatAdmin框架 使用phpword 生成word文件

下载phpword插件

composer require phpoffice/phpword

 生成word文件接口

static public function word()
    {
        //接收传值
        $order_id = request()->get('order_id');
        $tpl_id = request()->get('tpl_id');

        //查询出对应的数据以及关联数据
        $sale_order = \App\Models\SaleOrder::with(['customer','items','user.employee','amount'])->where('id',$order_id)->first();

        //我的模版存储在一个单独的表里,查询出使用的那一个模版文件
        $modelcontract = ContractTplModel::where('id',$tpl_id)->first();
        $file = $modelcontract->file;

        //循环产品数据存入变量中
        foreach($sale_order->items as $key => $val){
            $item_data[$key]['id'] = $key+1;
            $item_data[$key]['item_id'] =  $val->sku_id;
            $item_data[$key]['goods_name'] = $val->sku->goods_name ?? '';
            $item_data[$key]['sku_name'] = $val->sku->sku_name ?? '';
            $item_data[$key]['num'] = $val->should_num ?? '';
            $item_data[$key]['after_tax_price'] = $val->after_tax_price ?? '';
            $item_data[$key]['point_price'] = $val->point_price ?? '';
            $item_data[$key]['point'] = $val->point ?? '';
            $goods_name[] = $val->sku->goods_name ?? '';

        }
        $trade_name = implode("、", array_unique($goods_name));
        
        //获取模版文件完整的路径
        $file_path = storage_path('app/public/' . $file);

        //实例化phpword内置控制器,传值模版文件路径(重要,一定要加这一行实例化)
        $templateProcessor = new TemplateProcessor($file_path);

        //这里是变量取值,根据自己的数据赋值即可
        $party_a = $sale_order->customer->name;//甲方名称
        $product_name = $trade_name;//产品名称
        $total_amount = $sale_order->total_amount ?? ''; //含税总价
        $first_price = $total_amount * 0.3;
        $tow_price = $total_amount * 0.6 - $first_price;
//        dd($after_tax_price,$first_price,$tow_price);
        $price3 = $total_amount - $first_price - $tow_price;
        $support = new Support();
        $total_amount_cn = $support->convertAmountToCn($sale_order->total_amount);
        //含税总价大写
        $first_price_cn = $support->convertAmountToCn($first_price);
        $tow_price_cn = $support->convertAmountToCn($tow_price);
        $price3_cn = $support->convertAmountToCn($price3);

        $prefix = str_replace('-','','sk'.$sale_order->user->prefix.build_ws_no());//合同前缀
    
        //页面赋值,将页面上对应的值替换
        $templateProcessor->setValues(array(
            '合同编号'=>$prefix,
            '甲方名称'=>$party_a,
            '产品名称'=>$product_name,
            '含税总价'=>$sale_order->total_amount,
            '大写总价'=>$support->convertAmountToCn($sale_order->total_amount),
            '三个工作日内百分之'=>30,
            '第一笔金额'=>$first_price,
            '第一笔金额大写'=>$first_price_cn,
            '一笔货款发出时间'=>3,
            '收到货物工作日'=>2,
            '收到货物百分比'=>30,
            '第二笔金额'=>$tow_price,
            '第二笔金额大写'=>$tow_price_cn,
            '第二笔货款工作日'=>$tow_price,
            '第三笔货款'=>$price3,
            '第三笔货款大写'=>$price3_cn,
            '乙方收到款项后工作日'=>10,
            '甲方负责人姓名'=>$sale_order->customer->contacts_name??'',
            '甲方负责人电话'=>$sale_order->customer->contacts_mobile ?? '',
            '乙方负责人姓名'=>$sale_order->user->employee->name ?? '',
            '乙方负责人电话'=>$sale_order->user->employee->mobile ?? '',
            '增值税专用发票'=> 6 ?? '',
        ));
        
//这里是word模版文件中有表格,情况下的替换,如果你的模版文件没有表格到这里就不需要了,直接开始写生成文件即可。
        if (count($item_data)){
            $templateProcessor->cloneRow('id',count($item_data));

            foreach ($item_data as $k =>  $v) {
                $templateProcessor->setValue('id#'. ($k + 1), $v['id']);
                $templateProcessor->setValue('goods_name#'. ($k + 1), $v['goods_name']);
                $templateProcessor->setValue('sku_name#'. ($k + 1), $v['sku_name']);
                $templateProcessor->setValue('num#' . ($k + 1), $v['num']);
                $templateProcessor->setValue('after_tax_price#' . ($k + 1), $v['after_tax_price']);
                $templateProcessor->setValue('point#' . ($k + 1), $v['point']);
                $templateProcessor->setValue('point_price#' . ($k + 1), $v['point_price']);
            }
        }

        //生成word文件(保存路径)
        $templateProcessor->saveAs( storage_path().'/app/public/files/合同.docx');
    
        //下载文件
        $file_path = storage_path().'/app/public/files/合同.docx';  // 文件的路径和名称
        $file_name = basename($file_path);  // 获取文件名

        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . $file_name);

        ob_clean();flush();

        readfile($file_path);

    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值