PHP 简单运用phpword 固定模板填充内容

 public function Wordserver(){
        $session = yii::$app->session;
        //模板的路径,word的版本最好是docx,要不然可能会读取不了,根据自己的模板位置调整
        $filepath = \Yii::getAlias('@frontend/web/gagaword/');
        $path = $filepath."2212-打印头保险服务条款(用户保存版).docx";
        $type = ".docx";
        //生成word路径,根据自己的目录调整
        $fname = '2212-打印头保险服务条款(用户保存版)'.date('ymdHis',time()).rand(1000,9999).'.'.$type;//2212-打印头保险服务条款(用户保存版)2210271624249276.docx
        $session['gagawordname'] = $fname;//存起来  发邮件用
        $time = date('Ymd',time());
        $new_file = \Yii::getAlias('@frontend/web/wordfile/').$time."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0777);
        }

        $filePath = $new_file.$fname;

        //声明一个模板对象、读取模板
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($path);

        //替换模板中的变量,对应word里的 ${test}
        $customer_name = "ddddd";//用户信息
        $service_name  = "222";//销售单位
        $model = "333";//整机型号
        $host_sn = "444";//整机SN
        $number = "555";//打印头批号
        $service_type = "666";//购买服务类型
        $service_content = "vvvvvv";//购买服务内容
//        $test ="这是替换的内容";
//        $templateProcessor->setValue('test',$customer_name);//传真
        $templateProcessor->setValue('customer_name',$customer_name);
        $templateProcessor->setValue('service_name',$service_name);
        $templateProcessor->setValue('model',$model);
        $templateProcessor->setValue('host_sn',$host_sn);
        $templateProcessor->setValue('number',$number);
        $templateProcessor->setValue('service_type',$service_type);
        $templateProcessor->setValue('service_content',$service_content);

        //生成新的word
        $templateProcessor->saveAs($filePath);

    }

你可能感兴趣的:(PHP,php,word)