php 导出word文件

ps:前段时间项目有这个需求,在网上看到的做法 这里整理记录下

准备:

  1. composer 安装 PhpOffice/PhpWord
  2. 一份word 模板(提前写好变量占位)

(我理解的)原理:其实就是加载模板 替换变量 输出/保存

例图:
在这里插入图片描述

代码:

use PhpOffice\PhpWord\TemplateProcessor;
public function dayin(){

        $id = $this->request->param('id', 0, 'intval');
        $report_info =Db::name('project_report')->where("id", $id)->find();
        if(empty($report_info)){
            return json(['code'=>0,'msg'=>'网络延迟,请稍后重试']);
        }
        $tpl = public_path() . 'static/项目打印模板.docx';
        $doc = new TemplateProcessor($tpl);//打开模板

        $doc->setValue('project_no', $report_info['project_no']);
        $doc->setValue('project_name', $report_info['project_name']);
        $doc->setValue('create_time', date('Y-m-d',$report_info['create_time']));
        $doc->setValue('industry_type', $report_info['industry_type']);
        $doc->setValue('proposed_location', $report_info['proposed_location']);
        $doc->setValue('area_requirement', $report_info['area_requirement']);
        $doc->setValue('national_policy', $report_info['national_policy']);
        $doc->setValue('market_expectation', $report_info['market_expectation']);
        $doc->setValue('fixed_investment', $report_info['fixed_investment']);
        $doc->setValue('estimated_annual_tax', $report_info['estimated_annual_tax']);
        $doc->setValue('project_desc', $report_info['project_desc']);
        $doc->setValue('legal_person', $report_info['legal_person']);
        $doc->setValue('corporate_reputation', $report_info['corporate_reputation']);
        $doc->setValue('industry_position', $report_info['industry_position']);
        $doc->setValue('technological_innovation', $report_info['technological_innovation']);
        $doc->setValue('enterprise_desc', $report_info['enterprise_desc']);
        $doc->setValue('first_docking_people', $report_info['first_docking_people']);
        $doc->setValue('first_docking_people_mobile', $report_info['first_docking_people_mobile']);
        $doc->setValue('project_housekeeper', $report_info['project_housekeeper']);
        $doc->setValue('enterprise_principal', $report_info['enterprise_principal']);
        $doc->setValue('enterprise_principal_mobile', $report_info['enterprise_principal_mobile']);
        $doc->setValue('project_class', $report_info['project_class']);
        $doc->setValue('progress', $report_info['progress']);
        $doc->setValue('exist_problem', $report_info['exist_problem']);
        $doc->setValue('project_promotion_plan', $report_info['project_promotion_plan']);


        $deptA=$deptB=$deptC=$deptD=$deptE=$deptF = '□';

        switch ($report_info['project_status']){
            case 1:
                //$deptA = '☑';
                $deptA = '√';
                break;
            case 2:
                $deptB = '√';
                break;
            case 3:
                $deptB = '√';
                break;
            case 4:
                $deptB = '√';
                break;
            case 5:
                $deptB = '√';
                break;
            case 6:
                $deptB = '√';
                break;
            default:
                break;
        }
        $doc->setValue('deptA', $deptA);
        $doc->setValue('deptB', $deptB);
        $doc->setValue('deptC', $deptC);
        $doc->setValue('deptD', $deptD);
        $doc->setValue('deptE', $deptE);
        $doc->setValue('deptF', $deptF);


//        $doc->saveAs('test.docx');//另存为
//        return json(['code'=>1]);
        $filepath = $doc->save();
        $fileName = "项目基本情况.docx";

        // 下载文件
        ob_clean();
        header('Pragma: public');
        header("Access-Control-Expose-Headers: Content-Disposition");
        header("Accept-Ranges: bytes");
        header('ResponseType: blob');
        header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        header('Content-type:multipart/form-data');
        header("Content-Disposition:attachment; filename=" . urlencode($fileName));//设置下载的默认文件名
        header('Content-length:' . filesize($filepath));
        $fp = fopen($filepath, 'r');
        while (connection_status() == 0 && $buf = @fread($fp, 8192)) {
            echo $buf;
        }
        fclose($fp);
        @unlink($filepath);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阿巳

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值