ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-03-06 21:49:17  阅读:324  来源: 互联网

标签:docx word zip PHPWord id PATH 文件批量 ROOT DS


    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;

    }

 

标签:docx,word,zip,PHPWord,id,PATH,文件批量,ROOT,DS
来源: https://blog.csdn.net/qq_33382313/article/details/88258388

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有