php生成word文档

php生成word文档

/**
     * @title phpword样式
     */
    public function phpWord1($g)
    {
        $twips = 20; //单位 1磅 == 20缇
        $row = 15.6*$twips; //一行等于15.6磅
        $pound_6 = 6*$twips; //6磅
        $pound_26 = 26*$twips; //26磅
        $firstLine = 0.8;
        // 实例化 
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        //添加默认页面(布局默认页面方向和页边距):
        $section = $phpWord->addSection();
        //设置字体以及大小
        $phpWord->setDefaultFontName('宋体');
        $phpWord->setDefaultFontSize(14);
        //添加样式
        $titleFontStyle = [];
        $paragraphStyle = [];
        //标题样式
        $firstFontStyle = ['size'=>16,'name'=>'黑体'];
        $firstParagraphStyle = ['spaceBefore'=>$row,'spaceAfter'=>$pound_6];
        $secondFontStyle = ['size'=>15,'name'=>'黑体'];
        $secondParagraphStyle = ['spaceBefore'=>$pound_6,'spaceAfter'=>$pound_6];
        $thirdFontStyle = ['size'=>14,'name'=>'黑体','bold'=>true];
        $zwParagraphStyle = ['indent'=>$firstLine];
        // $thirdParagraphStyle = ['spaceBefore'=>$pound_6,'spaceAfter'=>$pound_6];

        $section->addText('一、标题',$firstFontStyle,$firstParagraphStyle);
        $section->addText('1.主标题',$secondFontStyle,$secondParagraphStyle);
        $section->addText($g['main_title'],null,$zwParagraphStyle);//正文
        $section->addText('2.副标题',$secondFontStyle,$secondParagraphStyle);
        $section->addText($g['noun_title'],null,$zwParagraphStyle);//正文
        $section->addText('3.项目基本信息',$secondFontStyle,$secondParagraphStyle);
        $section->addText($g['basic_info'],null,$zwParagraphStyle);//正文
        $section->addText('二、项目概述',$firstFontStyle,$firstParagraphStyle);
        $section->addText($g['overview'],null,$zwParagraphStyle);//正文
        $section->addText('三、项目技术和优势详解',$firstFontStyle,$firstParagraphStyle);
        $section->addText('1.项目技术和服务',$secondFontStyle,$secondParagraphStyle);
        $section->addText('(1)项目依据的技术',$thirdFontStyle,$zwParagraphStyle);
        $section->addText($g['position'],null,$zwParagraphStyle);//正文
        $section->addText('(2)项目产品或服务',$thirdFontStyle,$zwParagraphStyle);
        $section->addText($g['distribution'],null,$zwParagraphStyle);//正文

  
        $file = 'C.docx';
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save("php://output");
        exit;
    }
posted @   刘俊涛的博客  阅读(655)  评论(0编辑  收藏  举报
历史上的今天:
2021-03-10 MYSQL 导出excel bigint 精度丢失
2017-03-10 Win10怎么设置点击任务栏上文件夹图标直接打开“我的电脑”?
2017-03-10 SSO是什么?
2017-03-10 PHPWIND和DISCUZ什么区别?
2017-03-10 如何去掉显卡的桌面右键菜单:
2017-03-10 Php优化方案
2017-03-10 PHP中静态方法(static)与非静态方法的使用及区别
Servbay
点击右上角即可分享
微信分享提示