php 根据固定模版生成word

第一步 下载一个phpword扩展如图示例

第二步上实例

public function index(){
//        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        // New portrait section
        $id = input('id','','trim');
        $list = Db::name('information')->where('id',$id)->find();
        $type = $list['type'];
        $host  = app()->getRootPath();  //项目绝对路径
        $rootPath  = $host.'word_tmp/';  //项目绝对路径
        if($type == 1){
            $path = $rootPath.'tmp1.docx'; //模版1 
        }elseif($type == 2){
            $path = $rootPath.'tpm2.docx'; //模版2 
        }elseif($type == 2){
            $path = $rootPath.'tmp3.docx'; //模版3 
        }else{
            $path = $rootPath.'tmp4.docx'; //模版4 
        }

        $imagePath = $host.'public/';  //图片绝对路径
//        $image1 = $host.'/public/uploads/api/202408/66af48a261c4f.jpg';
//        $image2 = $host.'/public/uploads/api/202408/66af48a11264f.jpg';
//        $image3 = $host.'/public/uploads/api/202408/66af48a05b837.jpg';
//        dd($path);
//        $path = "tpl.doc";  //指明你的word模板地址



        $phpWordProcessor = new \PhpOffice\PhpWord\TemplateProcessor($path); //加载模板文件

//        $phpWordProcessor->writeImage($image1); // 写入图片文件
        // 只渲染
        // 发动机图片
        $width = 500;
        $height = 500;
        $engine_describe_image1 = json_decode($list['engine_describe'],true)['image'];
        if(!empty($engine_describe_image1[0])){
            $phpWordProcessor->setImageValue('engine_describe_image1', ['path' => $imagePath.self::image($engine_describe_image1[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('engine_describe_image1','');
        }
        if(!empty($engine_describe_image1[1])){
            $phpWordProcessor->setImageValue('engine_describe_image2', ['path' => $imagePath.self::image($engine_describe_image1[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('engine_describe_image2','');
        }
//        dd(10010);
        if(!empty($engine_describe_image1[2])){
            $phpWordProcessor->setImageValue('engine_describe_image3',['path' => $imagePath.self::image($engine_describe_image1[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('engine_describe_image3','');
        }

//         // 电气描述1
        $dq_describe1 = json_decode($list['dq_describe1'],true)['image'];
        if(!empty($dq_describe1[0]['url'])){
            $phpWordProcessor->setImageValue('dq_describe1_image1',['path' => $imagePath.self::image($dq_describe1[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe1_image1','');
        }
        if(!empty($dq_describe1[1]['url'])){
            $phpWordProcessor->setImageValue('dq_describe1_image2',['path' => $imagePath.self::image($dq_describe1[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe1_image2','');
        }
        if(!empty($dq_describe1[2]['url'])){
            $phpWordProcessor->setImageValue('dq_describe1_image3',['path' => $imagePath.self::image($dq_describe1[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe1_image3','');
        }
//        // 电气描述2
        $dq_describe2 = json_decode($list['dq_describe2'],true)['image'];
        if(!empty($dq_describe2[0]['url'])){
            $phpWordProcessor->setImageValue('dq_describe2_image1',['path' => $imagePath.self::image($dq_describe2[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe2_image1','');
        }
        if(!empty($dq_describe2[1]['url'])){
            $phpWordProcessor->setImageValue('dq_describe2_image2',['path' => $imagePath.self::image($dq_describe2[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe2_image2','');
        }
        if(!empty($dq_describe2[2]['url'])){
            $phpWordProcessor->setImageValue('dq_describe2_image3',['path' => $imagePath.self::image($dq_describe2[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe2_image3','');
        }
//        dd(21000);
//        // 电气描述3
//        dd(json_decode($list['dq_describe3'],true));
        $dq_describe3 = json_decode($list['dq_describe3'],true)['image'];
        if(!empty($dq_describe3[0]['url'])){
            $phpWordProcessor->setImageValue('dq_describe3_image1',['path' => $imagePath.self::image($dq_describe3[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe3_image1','');
        }
        if(!empty($dq_describe3[1]['url'])){
            $phpWordProcessor->setImageValue('dq_describe3_image2',['path' => $imagePath.self::image($dq_describe3[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe3_image2','');
        }
        if(!empty($dq_describe3[2]['url'])){
            $phpWordProcessor->setImageValue('dq_describe3_image3',['path' => $imagePath.self::image($dq_describe3[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('dq_describe3_image3','');
        }

        // 液压描述1
        $yy_describe1 = json_decode($list['yy_describe1'],true)['image'];
        if(!empty($yy_describe1[0]['url'])){
            $phpWordProcessor->setImageValue('yy_describe1_image1',['path' => $imagePath.self::image($yy_describe1[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe1_image1','');
        }
        if(!empty($yy_describe1[1]['url'])){
            $phpWordProcessor->setImageValue('yy_describe1_image2',['path' => $imagePath.self::image($yy_describe1[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe1_image2','');
        }
        if(!empty($yy_describe1[2]['url'])){
            $phpWordProcessor->setImageValue('yy_describe1_image3',['path' => $imagePath.self::image($yy_describe1[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe1_image3','');
        }
        // 液压描述2
        $yy_describe2 = json_decode($list['yy_describe2'],true)['image'];
        if(!empty($yy_describe2[0]['url'])){
            $phpWordProcessor->setImageValue('yy_describe2_image1',['path' => $imagePath.self::image($yy_describe2[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe2_image1','');
        }
        if(!empty($yy_describe2[1]['url'])){
            $phpWordProcessor->setImageValue('yy_describe2_image2',['path' => $imagePath.self::image($yy_describe2[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe2_image2','');
        }
        if(!empty($yy_describe2[2]['url'])){
            $phpWordProcessor->setImageValue('yy_describe2_image3',['path' => $imagePath.self::image($yy_describe2[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe2_image3','');
        }
        // 液压描述3
        $yy_describe3 = json_decode($list['yy_describe3'],true)['image'];
        if(!empty($yy_describe3[0]['url'])){
            $phpWordProcessor->setImageValue('yy_describe3_image1',['path' => $imagePath.self::image($yy_describe3[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe3_image1','');
        }
        if(!empty($yy_describe3[1]['url'])){
            $phpWordProcessor->setImageValue('yy_describe3_image2',['path' => $imagePath.self::image($yy_describe3[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe3_image2','');
        }
        if(!empty($yy_describe3[2]['url'])){
            $phpWordProcessor->setImageValue('yy_describe3_image3',['path' => $imagePath.self::image($yy_describe3[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('yy_describe3_image3','');
        }
        // 机械描述1
        $jx_describe1 = json_decode($list['jx_describe1'],true)['image'];
        if(!empty($jx_describe1[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe1_image1',['path' => $imagePath.self::image($jx_describe1[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe1_image1','');
        }
        if(!empty($jx_describe1[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe1_image2',['path' => $imagePath.self::image($jx_describe1[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe1_image2','');
        }
        if(!empty($jx_describe1[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe1_image3',['path' => $imagePath.self::image($jx_describe1[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe1_image3','');
        }
        // 机械描述2
        $jx_describe2 = json_decode($list['jx_describe2'],true)['image'];
        if(!empty($jx_describe2[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe2_image1',['path' => $imagePath.self::image($jx_describe2[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe2_image1','');
        }
        if(!empty($jx_describe2[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe2_image2',['path' => $imagePath.self::image($jx_describe2[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe2_image2','');
        }
        if(!empty($jx_describe2[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe2_image3',['path' => $imagePath.self::image($jx_describe2[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe2_image3','');
        }
        // 机械描述3
        $jx_describe3 = json_decode($list['jx_describe3'],true)['image'];
        if(!empty($jx_describe3[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe3_image1',['path' => $imagePath.self::image($jx_describe3[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe3_image1','');
        }
        if(!empty($jx_describe3[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe3_image2',['path' => $imagePath.self::image($jx_describe3[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe3_image2','');
        }
        if(!empty($jx_describe3[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe3_image3',['path' => $imagePath.self::image($jx_describe3[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe3_image3','');
        }
        // 机械描述4
        $jx_describe4 = json_decode($list['jx_describe4'],true)['image'];
        if(!empty($jx_describe4[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe4_image1',['path' => $imagePath.self::image($jx_describe4[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe4_image1','');
        }
        if(!empty($jx_describe4[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe4_image2',['path' => $imagePath.self::image($jx_describe4[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe4_image2','');
        }
        if(!empty($jx_describe4[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe4_image3',['path' => $imagePath.self::image($jx_describe4[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe4_image3','');
        }
        // 机械描述5
        $jx_describe5 = json_decode($list['jx_describe5'],true)['image'];
        if(!empty($jx_describe5[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe5_image1',['path' => $imagePath.self::image($jx_describe5[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe5_image1','');
        }
        if(!empty($jx_describe5[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe5_image2',['path' => $imagePath.self::image($jx_describe5[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe5_image2','');
        }
        if(!empty($jx_describe5[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe5_image3',['path' => $imagePath.self::image($jx_describe5[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe5_image3','');
        }
        // 机械描述6
        $jx_describe6 = json_decode($list['jx_describe6'],true)['image'];
        if(!empty($jx_describe6[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe6_image1',['path' => $imagePath.self::image($jx_describe6[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe6_image1','');
        }
        if(!empty($jx_describe6[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe6_image2',['path' => $imagePath.self::image($jx_describe6[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe6_image2','');
        }
        if(!empty($jx_describe6[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe6_image3',['path' => $imagePath.self::image($jx_describe6[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe6_image3','');
        }
        // 机械描述7
        $jx_describe7 = json_decode($list['jx_describe7'],true)['image'];
        if(!empty($jx_describe7[0]['url'])){
            $phpWordProcessor->setImageValue('jx_describe7_image1',['path' => $imagePath.self::image($jx_describe7[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe7_image1','');
        }
        if(!empty($jx_describe7[1]['url'])){
            $phpWordProcessor->setImageValue('jx_describe7_image2',['path' => $imagePath.self::image($jx_describe7[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe7_image2','');
        }
        if(!empty($jx_describe7[2]['url'])){
            $phpWordProcessor->setImageValue('jx_describe7_image3',['path' => $imagePath.self::image($jx_describe7[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('jx_describe7_image3','');
        }
        // part_list
        $part_list = json_decode($list['part_list'],true)['image'];
        if(!empty($part_list[0]['url'])){
            $phpWordProcessor->setImageValue('part_list_image1',['path' => $imagePath.self::image($part_list[0]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('part_list_image1','');
        }
        if(!empty($part_list[1]['url'])){
            $phpWordProcessor->setImageValue('part_list_image2',['path' => $imagePath.self::image($part_list[1]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('part_list_image2','');
        }
        if(!empty($part_list[2]['url'])){
            $phpWordProcessor->setImageValue('part_list_image3',['path' => $imagePath.self::image($part_list[2]['url']),'width'=>$width,'height'=>$height]);
        }else{
            $phpWordProcessor->setValue('part_list_image3','');
        }
//       dd(4000);
        //        $phpWordProcessor->setImageValue('dq_describe1',json_decode($list['dq_describe1'],true)['text']);
//        $phpWordProcessor->setImageValue('dq_describe2',json_decode($list['dq_describe2'],true)['text']);
//        $phpWordProcessor->setImageValue('dq_describe3',json_decode($list['dq_describe3'],true)['text']);
//        $phpWordProcessor->setImageValue('yy_describe1',json_decode($list['yy_describe1'],true)['text']);
//        $phpWordProcessor->setImageValue('yy_describe2',json_decode($list['yy_describe2'],true)['text']);
//        $phpWordProcessor->setImageValue('yy_describe3',json_decode($list['yy_describe3'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe1',json_decode($list['jx_describe1'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe2',json_decode($list['jx_describe2'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe3',json_decode($list['jx_describe3'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe4',json_decode($list['jx_describe4'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe5',json_decode($list['jx_describe5'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe6',json_decode($list['jx_describe6'],true)['text']);
//        $phpWordProcessor->setImageValue('jx_describe7',json_decode($list['jx_describe7'],true)['text']);

//        $phpWordProcessor->setImageValue('dq_describe1',['path' => $image2,'width'=>300,'height'=>300,'left'=>100]);
//        $phpWordProcessor->setImageValue('dq_describe1',['path' => $image2,'width'=>300,'height'=>300,'left'=>100]);
// 设置图片宽高
//        $phpWordProcessor->setImageValue('dq_describe1', ['path' => $image2,'width'=>1500,'height'=>1500]);
// 设置多次替换
//        $phpWordProcessor ->setImageValue('dq_describe1', ['path' => $image2,'width'=>1500,'height'=>1500],3);

//        $phpWordProcessor->writeImage($image2); // 写入图片文件
//        $phpWordProcessor->writeImage($image3); // 写入图片文件

        $phpWordProcessor->setValue('company',$list['company']);
        $phpWordProcessor->setValue('serial_number',$list['serial_number']);
        $phpWordProcessor->setValue('models',$list['models']);
        $phpWordProcessor->setValue('serial_inspect_date',$list['serial_inspect_date']);
        $phpWordProcessor->setValue('inspect_date',$list['inspect_date']);
        $phpWordProcessor->setValue('engineer',$list['engineer']);
        $phpWordProcessor->setValue('customer_name',$list['customer_name']);
        $phpWordProcessor->setValue('customer_address',$list['customer_address']);
        $phpWordProcessor->setValue('inspection_date',$list['inspection_date']);
        $phpWordProcessor->setValue('Ironing_board',$list['Ironing_board']);
        $phpWordProcessor->setValue('start_date',$list['start_date']);
        $phpWordProcessor->setValue('hour',$list['hour']);
//        $host = $_SERVER['HTTP_HOST'];
//        dd(111);
        $phpWordProcessor->setValue('brand',$list['brand']);
        $phpWordProcessor->setValue('engine_describe',json_decode($list['engine_describe'],true)['text']);
        $phpWordProcessor->setValue('dq_describe1',json_decode($list['dq_describe1'],true)['text']);
        $phpWordProcessor->setValue('dq_describe2',json_decode($list['dq_describe2'],true)['text']);
        $phpWordProcessor->setValue('dq_describe3',json_decode($list['dq_describe3'],true)['text']);
        $phpWordProcessor->setValue('yy_describe1',json_decode($list['yy_describe1'],true)['text']);
        $phpWordProcessor->setValue('yy_describe2',json_decode($list['yy_describe2'],true)['text']);
        $phpWordProcessor->setValue('yy_describe3',json_decode($list['yy_describe3'],true)['text']);
        $phpWordProcessor->setValue('jx_describe1',json_decode($list['jx_describe1'],true)['text']);
        $phpWordProcessor->setValue('jx_describe2',json_decode($list['jx_describe2'],true)['text']);
        $phpWordProcessor->setValue('jx_describe3',json_decode($list['jx_describe3'],true)['text']);
        $phpWordProcessor->setValue('jx_describe4',json_decode($list['jx_describe4'],true)['text']);
        $phpWordProcessor->setValue('jx_describe5',json_decode($list['jx_describe5'],true)['text']);
        $phpWordProcessor->setValue('jx_describe6',json_decode($list['jx_describe6'],true)['text']);
        $phpWordProcessor->setValue('jx_describe7',json_decode($list['jx_describe7'],true)['text']);
        $phpWordProcessor->setValue('other_describe',$list['other_describe']);
        $phpWordProcessor->setValue('patrol_date',$list['patrol_date']);
        $phpWordProcessor->setValue('accessory_name',$list['accessory_name']);
        $phpWordProcessor->setValue('accessory_phone',$list['accessory_phone']);
        $phpWordProcessor->setValue('give_name',$list['give_name']);
        $phpWordProcessor->setValue('give_phone',$list['give_phone']);
        // 模版4
        $phpWordProcessor->setValue('work_order',$list['work_order']);
        $phpWordProcessor->setValue('contacts_name',$list['contacts_name']);
        $phpWordProcessor->setValue('contacts_phone',$list['contacts_phone']);
        $phpWordProcessor->setValue('address',$list['address']);
        $phpWordProcessor->setValue('sketch',$list['sketch']);
        $phpWordProcessor->setValue('jiancha',$list['jiancha']);
        $phpWordProcessor->setValue('baoyang',$list['baoyang']);
        $phpWordProcessor->setValue('peijian',$list['peijian']);
        $phpWordProcessor->setValue('qita',$list['qita']);
        $phpWordProcessor->setValue('zongji',$list['zongji']);
//        零件清单开始
        $phpWordProcessor->setValue('serial1_name',json_decode($list['part'],true)['serial1']['name']);
        $phpWordProcessor->setValue('serial1_price',json_decode($list['part'],true)['serial1']['price']);
        $phpWordProcessor->setValue('serial1_num',json_decode($list['part'],true)['serial1']['num']);
        $phpWordProcessor->setValue('serial1_total',json_decode($list['part'],true)['serial1']['total']);
        $phpWordProcessor->setValue('serial1_source',json_decode($list['part'],true)['serial1']['source']);
        $phpWordProcessor->setValue('serial1_notes',json_decode($list['part'],true)['serial1']['notes']);

        $phpWordProcessor->setValue('serial2_name',json_decode($list['part'],true)['serial2']['name']);
        $phpWordProcessor->setValue('serial2_price',json_decode($list['part'],true)['serial2']['price']);
        $phpWordProcessor->setValue('serial2_num',json_decode($list['part'],true)['serial2']['num']);
        $phpWordProcessor->setValue('serial2_total',json_decode($list['part'],true)['serial2']['total']);
        $phpWordProcessor->setValue('serial2_source',json_decode($list['part'],true)['serial2']['source']);
        $phpWordProcessor->setValue('serial2_notes',json_decode($list['part'],true)['serial2']['notes']);

        $phpWordProcessor->setValue('serial3_name',json_decode($list['part'],true)['serial3']['name']);
        $phpWordProcessor->setValue('serial3_price',json_decode($list['part'],true)['serial3']['price']);
        $phpWordProcessor->setValue('serial3_num',json_decode($list['part'],true)['serial3']['num']);
        $phpWordProcessor->setValue('serial3_total',json_decode($list['part'],true)['serial3']['total']);
        $phpWordProcessor->setValue('serial3_source',json_decode($list['part'],true)['serial3']['source']);
        $phpWordProcessor->setValue('serial3_notes',json_decode($list['part'],true)['serial3']['notes']);

        $phpWordProcessor->setValue('serial4_name',json_decode($list['part'],true)['serial4']['name']);
        $phpWordProcessor->setValue('serial4_price',json_decode($list['part'],true)['serial4']['price']);
        $phpWordProcessor->setValue('serial4_num',json_decode($list['part'],true)['serial4']['num']);
        $phpWordProcessor->setValue('serial4_total',json_decode($list['part'],true)['serial4']['total']);
        $phpWordProcessor->setValue('serial4_source',json_decode($list['part'],true)['serial4']['source']);
        $phpWordProcessor->setValue('serial4_notes',json_decode($list['part'],true)['serial4']['notes']);

        $phpWordProcessor->setValue('serial5_name',json_decode($list['part'],true)['serial5']['name']);
        $phpWordProcessor->setValue('serial5_price',json_decode($list['part'],true)['serial5']['price']);
        $phpWordProcessor->setValue('serial5_num',json_decode($list['part'],true)['serial5']['num']);
        $phpWordProcessor->setValue('serial5_total',json_decode($list['part'],true)['serial5']['totals']);
        $phpWordProcessor->setValue('serial5_source',json_decode($list['part'],true)['serial5']['source']);
        $phpWordProcessor->setValue('serial5_notes',json_decode($list['part'],true)['serial5']['notes']);

        $phpWordProcessor->setValue('serial6_name',json_decode($list['part'],true)['serial6']['name']);
        $phpWordProcessor->setValue('serial6_price',json_decode($list['part'],true)['serial6']['price']);
        $phpWordProcessor->setValue('serial6_num',json_decode($list['part'],true)['serial6']['num']);
        $phpWordProcessor->setValue('serial6_total',json_decode($list['part'],true)['serial6']['total']);
        $phpWordProcessor->setValue('serial6_source',json_decode($list['part'],true)['serial6']['source']);
        $phpWordProcessor->setValue('serial6_notes',json_decode($list['part'],true)['serial6']['notes']);

        $phpWordProcessor->setValue('serial7_name',json_decode($list['part'],true)['serial7']['name']);
        $phpWordProcessor->setValue('serial7_price',json_decode($list['part'],true)['serial7']['price']);
        $phpWordProcessor->setValue('serial7_num',json_decode($list['part'],true)['serial7']['num']);
        $phpWordProcessor->setValue('serial7_total',json_decode($list['part'],true)['serial7']['total']);
        $phpWordProcessor->setValue('serial7_source',json_decode($list['part'],true)['serial7']['source']);
        $phpWordProcessor->setValue('serial7_notes',json_decode($list['part'],true)['serial7']['notes']);

        $phpWordProcessor->setValue('serial8_name',json_decode($list['part'],true)['serial8']['name']);
        $phpWordProcessor->setValue('serial8_price',json_decode($list['part'],true)['serial8']['price']);
        $phpWordProcessor->setValue('serial8_num',json_decode($list['part'],true)['serial8']['num']);
        $phpWordProcessor->setValue('serial8_total',json_decode($list['part'],true)['serial8']['total']);
        $phpWordProcessor->setValue('serial8_source',json_decode($list['part'],true)['serial8']['source']);
        $phpWordProcessor->setValue('serial8_notes',json_decode($list['part'],true)['serial8']['notes']);

        $phpWordProcessor->setValue('serial9_name',json_decode($list['part'],true)['serial9']['name']);
        $phpWordProcessor->setValue('serial9_price',json_decode($list['part'],true)['serial9']['price']);
        $phpWordProcessor->setValue('serial9_num',json_decode($list['part'],true)['serial9']['num']);
        $phpWordProcessor->setValue('serial9_total',json_decode($list['part'],true)['serial9']['total']);
        $phpWordProcessor->setValue('serial9_source',json_decode($list['part'],true)['serial9']['source']);
        $phpWordProcessor->setValue('serial9_notes',json_decode($list['part'],true)['serial9']['notes']);

        $phpWordProcessor->setValue('serial10_name',json_decode($list['part'],true)['serial10']['name']);
        $phpWordProcessor->setValue('serial10_price',json_decode($list['part'],true)['serial10']['price']);
        $phpWordProcessor->setValue('serial10_num',json_decode($list['part'],true)['serial10']['num']);
        $phpWordProcessor->setValue('serial10_total',json_decode($list['part'],true)['serial10']['total']);
        $phpWordProcessor->setValue('serial10_source',json_decode($list['part'],true)['serial10']['source']);
        $phpWordProcessor->setValue('serial10_notes',json_decode($list['part'],true)['serial10']['notes']);

        $phpWordProcessor->setValue('serial11_name',json_decode($list['part'],true)['serial11']['name']);
        $phpWordProcessor->setValue('serial11_price',json_decode($list['part'],true)['serial11']['price']);
        $phpWordProcessor->setValue('serial11_num',json_decode($list['part'],true)['serial11']['num']);
        $phpWordProcessor->setValue('serial11_total',json_decode($list['part'],true)['serial11']['total']);
        $phpWordProcessor->setValue('serial11_source',json_decode($list['part'],true)['serial11']['source']);
        $phpWordProcessor->setValue('serial11_notes',json_decode($list['part'],true)['serial11']['notes']);

        $phpWordProcessor->setValue('serial12_name',json_decode($list['part'],true)['serial12']['name']);
        $phpWordProcessor->setValue('serial12_price',json_decode($list['part'],true)['serial12']['price']);
        $phpWordProcessor->setValue('serial12_num',json_decode($list['part'],true)['serial12']['num']);
        $phpWordProcessor->setValue('serial12_total',json_decode($list['part'],true)['serial12']['total']);
        $phpWordProcessor->setValue('serial12_source',json_decode($list['part'],true)['serial12']['source']);
        $phpWordProcessor->setValue('serial12_notes',json_decode($list['part'],true)['serial12']['notes']);

        $phpWordProcessor->setValue('serial13_name',json_decode($list['part'],true)['serial13']['name']);
        $phpWordProcessor->setValue('serial13_price',json_decode($list['part'],true)['serial13']['price']);
        $phpWordProcessor->setValue('serial13_num',json_decode($list['part'],true)['serial13']['num']);
        $phpWordProcessor->setValue('serial13_total',json_decode($list['part'],true)['serial13']['total']);
        $phpWordProcessor->setValue('serial13_source',json_decode($list['part'],true)['serial13']['source']);
        $phpWordProcessor->setValue('serial13_notes',json_decode($list['part'],true)['serial13']['notes']);

        $phpWordProcessor->setValue('serial14_name',json_decode($list['part'],true)['serial14']['name']);
        $phpWordProcessor->setValue('serial14_price',json_decode($list['part'],true)['serial14']['price']);
        $phpWordProcessor->setValue('serial14_num',json_decode($list['part'],true)['serial14']['num']);
        $phpWordProcessor->setValue('serial14_total',json_decode($list['part'],true)['serial14']['total']);
        $phpWordProcessor->setValue('serial14_source',json_decode($list['part'],true)['serial14']['source']);
        $phpWordProcessor->setValue('serial14_notes',json_decode($list['part'],true)['serial14']['notes']);

        $phpWordProcessor->setValue('serial15_name',json_decode($list['part'],true)['serial15']['name']);
        $phpWordProcessor->setValue('serial15_price',json_decode($list['part'],true)['serial15']['price']);
        $phpWordProcessor->setValue('serial15_num',json_decode($list['part'],true)['serial15']['num']);
        $phpWordProcessor->setValue('serial15_total',json_decode($list['part'],true)['serial15']['total']);
        $phpWordProcessor->setValue('serial15_source',json_decode($list['part'],true)['serial15']['source']);
        $phpWordProcessor->setValue('serial15_notes',json_decode($list['part'],true)['serial15']['notes']);

        $phpWordProcessor->setValue('heji',$list['heji']);

//        零件清单结束
        $times = date('Y-m-d',$list['addtime']);
        if($type == 1){
//            $path = $rootPath.'tmp1.docx'; //模版1 
            $filename = iconv("utf-8","gb2312",$times."模版1".$list['models']);   //防止有的浏览器下载之后中文乱码
        }elseif($type == 2){
//            $path = $rootPath.'tpm2.docx'; //模版2 
            $filename = iconv("utf-8","gb2312",$times."模版2".$list['models']);   //防止有的浏览器下载之后中文乱码
        }elseif($type == 3){
//            $path = $rootPath.'tmp3.docx'; //模版3 
            $filename = iconv("utf-8","gb2312",$times."模版3".$list['models']);   //防止有的浏览器下载之后中文乱码
        }else{
            $filename = iconv("utf-8","gb2312",$times."模版4".$list['models']);   //防止有的浏览器下载之后中文乱码
        }
//        dd(111);
//        dd($filename);
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=".$filename.".docx");
        header('Cache-Control: max-age=0');

        $phpWordProcessor->saveAs("php://output");
//        $section = $phpWord->addSection(array('orientation' => 'portrait'));
//        // Add one empty table
//        $table = $section->addTable();
//        // Adding cell
//        $cell = $table->addRow()->addCell();
//        $textRun = $cell->addTextRun();
//        $textRun->addText("Hello World!");



//        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWordProcessor, 'Word2007');
//        $objWriter->save('helloWorld.docx');
    }

然后直接访问这个方法就完事了,文档就生成出来了

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值