PhpWord

35 篇文章 0 订阅

    public function createWord()
    {
        $phpWord = new PhpWord();
        $cmScale = 567;
        $imageScale = 30;
        $pageH= 21 * $cmScale;
        $pageW = 29.7 * $cmScale;
        $section = $phpWord->addSection(
            ['pageSizeW' =>$pageW,
                'pageSizeH'=>$pageH,
                'orientation'=>'landscape',
                'marginTop'=>0.3*$cmScale,
                'marginLeft'=>0.3*$cmScale,
                'marginRight'=>0.3*$cmScale,
                'marginBottom'=>0.3*$cmScale,
                ]
        );

        $pageH-=0.6*$cmScale;
        $pageW-=0.6*$cmScale;
        $table = $section->addTable();

//添加行
        $table->addRow($pageH/2-100);
//添加行
        $cell = $table->addCell($pageW/4);
        $cell->addText('你好啊,111');


        $cell = $table->addCell($pageW/4);
        $cell->addText('乌拉拉222');

        $cell = $table->addCell($pageW/4);
        $cell->addText('你好啊333,');

        $cell = $table->addCell($pageW/4);
        $cell->addText('乌拉拉4444');

//例:

        $table->addRow($pageH/2-100);
        
        $cell = $table->addCell($pageW/4,array( 'bgColor'=>'66BBFF'));
        $cell->addTextBreak(1);
        $textrun1 = $cell->addTextRun(array( 'alignment' =>'center'));
        $textrun1->addText('六年级二班兑换积分');

        $cell->addTextBreak(2);
        
        $textrun2 = $cell->addTextRun(array( 'alignment' =>'center'));
        $textrun2->addText('内容');
        $textrun3 = $cell->addTextRun(array( 'alignment' =>'center'));
        $textrun3->addText('描述');
        $cell->addTextBreak(3);
        $imageStyle = array('width'=>3.4*$imageScale, 'height'=>3.4*$imageScale, 'align'=>'center');
        $cell->addImage('路径.png', $imageStyle);

        $textrun4 = $cell->addTextRun(array( 'alignment' =>'center'));
        $textrun4->addText('xxxx');

        $phpWord->save('文件名.doc','Word2007',true);
    }

html 生成word下载

#html生成word文档并下载
    /*
     * $arr: $content
     * string $fileName
     * */
    public function downloadWord($content, $fileName='new_file.doc'){
        if(empty($content)){
            return;
        }

        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$fileName");

        /*$html = '<html xmlns:v="urn:schemas-microsoft-com:vml"
         xmlns:o="urn:schemas-microsoft-com:office:office"
         xmlns:w="urn:schemas-microsoft-com:office:word" 
         xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" 
         xmlns="http://www.w3.org/TR/REC-html40">';*/

        $html  = '
        <html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:w="urn:schemas-microsoft-com:office:word"
        xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
        $html .= '<head><meta charset="UTF-8" /></head>';

        echo $html . '<body>'.$content .'</body></html>';
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值