thinkphp PHPWord数据导出word固定模板,ZipArchive文件批量压缩打包下载

    public function down($id){
        $res = Db::name('usera')->where('id',$id)->find();
        $imgs = htmlspecialchars_decode($res['imgs']);
        $imgs = json_decode($imgs,true);
        //dump($imgs);   
        //die;

        $pingjiabiao = htmlspecialchars_decode($res['pingjiabiao']);
        $pingjiabiao = json_decode($pingjiabiao,true);

        if (empty($pingjiabiao)) {
            $this->error('评价表未提交');
        }



        $PHPWord = new PhpWord();
        //生成评价表
        $templateProcessor = $PHPWord->loadTemplate(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjiamb.docx');
        foreach ($pingjiabiao as $k => $v) {
            $templateProcessor->setValue($k,$v);
        }
        $templateProcessor->saveAs(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjia_u'.$id.'.docx');


        $wenjuan = htmlspecialchars_decode($res['wenjuandiaocha']);
        $wenjuan = json_decode($wenjuan,true);
        if (empty($pingjiabiao)) {
            $this->error('问卷调查未提交');
        }

        //生成问卷调查
        $templateProcessor = $PHPWord->loadTemplate(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuanmb.docx');
        foreach ($wenjuan as $k => $v) {
            $templateProcessor->setValue($k,$v);
        }
        $templateProcessor->saveAs(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuan_u'.$id.'.docx');

        $filename = "user_".$id.".zip";
        // 生成文件
        $zip = new \ZipArchive ();
        // 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
        if ($zip->open ($filename ,\ZipArchive::OVERWRITE) !== true) {
            //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
            if($zip->open ($filename ,\ZipArchive::CREATE) !== true){
                // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
                exit ( '无法打开文件,或者文件创建失败' );
            }
        }

        //添加到压缩包
        $zip->addFile(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjia_u'.$id.'.docx',"评价表.docx");
        $zip->addFile(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuan_u'.$id.'.docx',"问卷调查.docx");
        foreach ($imgs as $v) {
            if(array_key_exists("up_img",$v)){
                $img = str_replace("http://xiedian.xxxxxx.cn","",$v['up_img']); 
                $zip->addFile(ROOT_PATH . $img,"images/".$v['title'].".jpg");
            }  
        }

        $video = str_replace("http://xiedian.xxxxxx.cn","",$res['video']); 
        $zip->addFile(ROOT_PATH . $video,"video/video.mp4");


        // 关闭
        $zip->close ();
        //下面是输出下载;
        header ( "Cache-Control: max-age=0" );
        header ( "Content-Description: File Transfer" );
        header ( 'Content-disposition: attachment; filename=' . basename ( $filename ) ); // 文件名
        header ( "Content-Type: application/zip" ); // zip格式的
        header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件
        header ( 'Content-Length: ' . filesize ( $filename ) ); // 告诉浏览器,文件大小
        @readfile ( $filename );//输出文件;
        exit;

    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值