php word文档中写入数据

php 复制代码
<?php

namespace app\api\controller;

/**
 * 首页接口
 */
class Coursess extends Api
{
    //签订合同
    public function contract()
    {
        $id = $this->request->post('id');
        $qian = $this->request->post('qian');
        
        if (!$id || !$qian) {
            $this->error(__('Invalid parameters'));
        }
        $order_house = OrderHouse::find($id);
        if (!$order_house) {
            $this->error(__('订单不存在'));
        }
        if ($order_house->contract == 1) {
            $this->error(__('合同已签署'));
        }
        $order = Order::where('order_first_id|order_two_id',$order_house->id)->find();
        if (!$order) {
            $this->error(__('订单不存在'));
        }
        
        $j_order = OrderHouse::find($order->order_first_id);
        if (!$j_order) {
           $this->error(__('订单不存在'));
        }
        $j_time = explode(' - ',$j_order->reservation);
        
        $date1 = new \DateTime($j_time[0]);
        $date2 = new \DateTime($j_time[1]);
        $interval = $date1->diff($date2);
        $day = $interval->format('%a');
        
        $s_time = explode('-',$j_time[0]);
        $e_time = explode('-',$j_time[1]);
        $j_user_real = UserReal::where('user_id',$j_order->user_id)->find();
        if (!$j_user_real) {
            $this->error(__('未找到实名信息'));
        }
        $y_order = OrderHouse::find($order->order_two_id);
        if (!$y_order) {
           $this->error(__('订单不存在'));
        }
        $y_user_real = UserReal::where('user_id',$y_order->user_id)->find();
        if (!$y_user_real) {
            $this->error(__('未找到实名信息'));
        }
        $data = [
            'j_name' => $j_user_real->username,
            'j_card' => $j_user_real->card,
            'y_name' => $y_user_real->username,
            'y_card' => $y_user_real->card,
            'j_address' => $j_order->house->address.$j_order->house->add_detailed,
            'j_mian' => $j_order->house->house_area,
            'y_address' => $y_order->house->address.$y_order->house->add_detailed,
            'y_mian' => $y_order->house->house_area,
            
            's_year' => $s_time[0],
            's_month' => $s_time[1],
            's_ri' => $s_time[2],
            
            'e_year' => $e_time[0],
            'e_month' => $e_time[1],
            'e_ri' => $e_time[2],
            'day' => $day,
            
            'k_year' => $s_time[0],
            'k_month' => $s_time[1],
            'k_ri' => $s_time[2],
            
            'j_dprice' => Order::getChineseNumber($j_order->price),
            'j_price' => $j_order->price,
            'y_dprice' => Order::getChineseNumber($y_order->price),
            'y_price' => $y_order->price,
            
            'j_mobile' => $j_order->user->mobile,
            'y_mobile' => $y_order->user->mobile,
            
            'q_y' => date('Y'),
            'q_m' => date('m'),
            'q_r' => date('d'),
            
        ];
        
        if ($order->contract != '') {
            $bg = $order->contract;
        }else{
            $bg = '/hfht.docx';
        }
        
        if ($id == $order->order_first_id) {
            $type = 1;
        }else{
            $type = 2;
        }
        
        $contract = Order::words($data,$bg,$type, $qian);
        // halt($contract);
        if ($contract) {
            $order->contract = $contract;
            $order->save();
            $order_house->contract = 1;
            $order_house->save();
            $this->success('签订成功');
        }else{
            $this->error(__('签订失败'));
        }
        
    }



    //合同填充
    public static function words($data, $bg, $type, $qian){
        
        $templatePath = ROOT_PATH."public".$bg;
        
        $tpl = new \PhpOffice\PhpWord\TemplateProcessor($templatePath);
        
        //填充数据
        foreach ($data as $key => $value) {
            $tpl->setValue($key, $value);
        }
        if ($type == 1) {
            $tpl->setImageValue('j_qian', array('path' => ROOT_PATH."public".$qian, 'width' => 50, 'height' => 50, 'ratio' => false));
            
        }else{
            $tpl->setImageValue('y_qian', array('path' => ROOT_PATH."public".$qian, 'width' => 50, 'height' => 50, 'ratio' => false));
            
        }
        
        // $tpl->setImageValue('y_qian', ROOT_PATH."public".$qian);
        
        //文件名
        // if ($type == 1) {
        //     $title = 'queren';
        // }else{
        //     $title = 'songji';
        // }
        $file = time() . '.docx';//文件名
        // $encoded_filename = urlencode($file); // 将文件名进行urlencode转码
        $url = ROOT_PATH.'public';
        $path = '/contract/'.str_replace('+', '%20', $file);
        $tpl->saveAs($url.$path);
        // return \fast\Http::sendToBrowser($path,true);
        return $path;
    }


}

文档样式

相关推荐
hairenjing11236 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
龙哥·三年风水8 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
Dingww101112 小时前
梧桐数据库中的网络地址类型使用介绍分享
数据库·oracle·php
Genius Kim15 小时前
SpringCloud Sentinel 服务治理详解
spring cloud·sentinel·php
初九之潜龙勿用19 小时前
C#结合JS解决Word添加无效位图导致进程停滞的问题
javascript·ui·c#·word·asp.net
原机小子19 小时前
城镇保障性住房管理:SpringBoot系统解决方案
数据库·spring boot·php
kali-Myon20 小时前
NewStarCTF2024-Week5-Web&Misc-WP
前端·python·学习·mysql·web安全·php·web
DK七七21 小时前
当今陪玩系统小程序趋势,陪玩系统源码搭建后的适用于哪些平台
小程序·php·uniapp
火星技术1 天前
Excel快速转换文档word工具
word·excel
tekin1 天前
vscode php Launch built-in server and debug, PHP内置服务xdebug调试,自定义启动参数配置使用示例
ide·vscode·php·launch.json·runtimeargs·php内置服务自定义参数