phpWord 生成

/**
     * 教育部国家奖学金申请审批表
     */
    public static function scholarship_1()
    {
        $PHPWord = new PhpWord();

        $section=$PHPWord->addSection();
        $PHPWord->setDefaultFontName('宋体');//设置文档默认字体
        $PHPWord->setDefaultFontSize(12);//设置文档默认字号

        $title='(2016—2017学年)国家奖学金申请审批表';

        $PHPWord->addParagraphStyle('pStyle', ['align'=>'center', 'spaceAfter'=>300]);//段落样式

        $section->addText('',['size'=>15]);

        $section->addText($title,['bold'=>true, 'size'=>18,'name'=>'黑体'], 'pStyle');

        $separate_1='  ';//一格空格
        $separate_2=$separate_1.$separate_1;//两格空格
        $separate_4=$separate_2.$separate_2;//四格空格
        $separate_8=$separate_4.$separate_4;//八格空格

        $school='学校:'.'XXXX学校';
        $department='院系:'.'XXXX学院';
        $student_id='学号:'.'111111111111';

        $section->addText($school.$separate_4.$department.$separate_4.$student_id,
            ['size'=>12,'bold'=>true,'name'=>'宋体'],
            ['align'=>'center','spaceAfter'=>100]);

        $tableStyle=['borderSize'=>6];

        $PHPWord->addTableStyle('table',$tableStyle);
        $table=$section->addTable('table');

        $cellStyle_0=['valign'=>'center','vMerge' => 'restart'];
        $cellStyle_1=['vMerge' => 'continue'];
        $cellStyle_2=['valign'=>'center'];

        $textPStyle_1=['align'=>'center'];


        /**
         * *********************************************************************************基本情况
         */

        //宽度  11580
        //----------------------------------------------------------------------------------
        $table->addRow(612.5);//2450

        $table->addCell(1000,$cellStyle_0)->addText('基本'.$separate_2.'情况',['bold'=>true],$textPStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('姓名',null,$textPStyle_1);
        $table->addCell(2040,['valign'=>'center','gridSpan' => 4])->addText('',null,$textPStyle_1);

        $table->addCell(1020,['valign'=>'center','gridSpan' => 2])->addText('性别',null,$textPStyle_1);
        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('',null,$textPStyle_1);

        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('出生年月',null,$textPStyle_1);
        $table->addCell(3060,['valign'=>'center','gridSpan' => 6])->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(612.5);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('政治面貌',null,$textPStyle_1);
        $table->addCell(2040,['valign'=>'center','gridSpan' => 4])->addText('',null,$textPStyle_1);

        $table->addCell(1020,['valign'=>'center','gridSpan' => 2])->addText('民族',null,$textPStyle_1);
        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('',null,$textPStyle_1);

        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('入学时间',null,$textPStyle_1);
        $table->addCell(3060,['valign'=>'center','gridSpan' => 6])->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(612.5);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('专业',null,$textPStyle_1);
        $table->addCell(2040,['valign'=>'center','gridSpan' => 4])->addText('',null,$textPStyle_1);

        $table->addCell(1020,['valign'=>'center','gridSpan' => 2])->addText('学制',null,$textPStyle_1);
        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('',null,$textPStyle_1);

        $table->addCell(1530,['valign'=>'center','gridSpan' => 3])->addText('联系电话',null,$textPStyle_1);
        $table->addCell(3060,['valign'=>'center','gridSpan' => 6])->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(612.5);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('身份证号',null,$textPStyle_1);

        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(510,$cellStyle_2)->addText('',null,$textPStyle_1);

        /**
         * *********************************************************************************学习情况
         */
        $table=$section->addTable('table');

        //----------------------------------------------------------------------------------

        $table->addRow(612.5);//2450

        $table->addCell(1000,$cellStyle_0)->addText('学习'.$separate_2.'情况',['bold'=>true],$textPStyle_1);

        $table->addCell(5290,$cellStyle_2)->addText('成绩排名:x/x(名称/总人数)',null,$textPStyle_1);
        $table->addCell(5290,$cellStyle_2)->addText('实行综合考评排名:是√;否 □',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(612.5);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(5290,$cellStyle_2)->addText('必修课x门,其中及格以上x门',null,$textPStyle_1);
        $table->addCell(5290,$cellStyle_2)->addText('如是,排名:x/x(名称/总人数)',null,$textPStyle_1);

        /**
         * *********************************************************************************大学期间主要获奖情况
         */

        $table=$section->addTable('table');
        //----------------------------------------------------------------------------------

        $table->addRow(612.5);//2450

        $table->addCell(1000,$cellStyle_0)->addText('大学'.$separate_2.'期间'.$separate_2.'主要'.$separate_2.'获奖'.$separate_2.'情况',['bold'=>true],$textPStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('日期',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('奖项名称',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('颁奖单位',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(650);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(650);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(650);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);

        //----------------------------------------------------------------------------------

        $table->addRow(650);

        $table->addCell(1000,$cellStyle_1);

        $table->addCell(1400,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);
        $table->addCell(4590,$cellStyle_2)->addText('',null,$textPStyle_1);

        /**
     * *********************************************************************************申请理由
     */

        $table=$section->addTable('table');
        //----------------------------------------------------------------------------------

        $table->addRow(5000);

        $cell=$table->addCell(1000,$cellStyle_0);
        $cell->addText('申请'.$separate_2.'理由',['bold'=>true],$textPStyle_1);
        $cell->addText('(200字)',['size'=>9],$textPStyle_1);

        $cell=$table->addCell(10580,$cellStyle_2);
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText($separate_4.$separate_4.'申请人签名(手签):',null,$textPStyle_1);
        $cell->addText('');
        $cell->addText($separate_8.$separate_8.'年'.$separate_2.'月'.$separate_2.'日',null,$textPStyle_1);

        /**
         * *********************************************************************************推荐理由
         */

        $table=$section->addTable('table');
        //----------------------------------------------------------------------------------

        $table->addRow(5000);

        $cell=$table->addCell(1000,$cellStyle_0);
        $cell->addText('推荐'.$separate_2.'理由',['bold'=>true],$textPStyle_1);
        $cell->addText('(100字)',['size'=>9],$textPStyle_1);

        $cell=$table->addCell(10580,$cellStyle_2);
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText($separate_4.$separate_4.'推荐人(辅导员或班主任)签名:',null,$textPStyle_1);
        $cell->addText('');
        $cell->addText($separate_8.$separate_8.'年'.$separate_2.'月'.$separate_2.'日',null,$textPStyle_1);

        /**
         * *********************************************************************************院(系)意见
         */

        $table=$section->addTable('table');
        //----------------------------------------------------------------------------------

        $table->addRow(4000);

        $cell=$table->addCell(1000,$cellStyle_0);
        $cell->addText('院'.$separate_2.'(系)'.$separate_2.'意'.$separate_2.'见',['bold'=>true],$textPStyle_1);

        $cell=$table->addCell(10580,$cellStyle_2);
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText($separate_4.$separate_4.'院系主管学生工作领导签名:',null,$textPStyle_1);
        $cell->addText('');
        $cell->addText($separate_4.$separate_4.$separate_4.$separate_4.'(院系公章)',null,$textPStyle_1);
        $cell->addText('');
        $cell->addText($separate_8.$separate_8.'年'.$separate_2.'月'.$separate_2.'日',null,$textPStyle_1);

        /**
         * *********************************************************************************学校意见
         */

        $table=$section->addTable('table');
        //----------------------------------------------------------------------------------

        $table->addRow(4000);

        $cell=$table->addCell(1000,$cellStyle_0);
        $cell->addText('学'.$separate_2.'校'.$separate_2.'意'.$separate_2.'见',['bold'=>true],$textPStyle_1);

        $cell=$table->addCell(10580,$cellStyle_2);
        $cell->addText($separate_2.'经评审,并在校内公示x个工作日,无异议,现报请批准该同学获得国家奖学金。');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText('');
        $cell->addText($separate_4.$separate_4.'(学校公章)',null,$textPStyle_1);
        $cell->addText('');
        $cell->addText($separate_8.$separate_8.'年'.$separate_2.'月'.$separate_2.'日',null,$textPStyle_1);

        $section->addText('');
        $section->addText('制表:全国学生资助管理中心 2010年版',['size'=>10.5],['align'=>'right']);

        /**
         * *********************************************************************************输出
         */

        $filename=time().'.docx';

        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('php://output');

    }

PHPWord是一个用于创建Microsoft Office Word文档的PHP库,它允许开发者通过纯PHP代码动态生成Word文档,包括插入图表。要使用PHPWord生成图表,你可以按照以下步骤操作: 1. **安装与引入库**:首先需要从GitHub或其他官方仓库下载并安装phpoffice/phpword库。你可以使用Composer来管理依赖。 ```sh composer require phpoffice/phpword ``` 2. **实例化文档**:创建一个新的`PhpOffice\PhpWord\Document`对象。 ```php use PhpOffice\PhpWord\PhpWord; $document = new PhpWord(); ``` 3. **添加图表数据**:你需要准备一些数据,可以是数组或者其他可以转换为图表的数据源。 4. **创建图表对象**:使用`PhpOffice\PhpWord\SharedDrawing`或`PhpOffice\PhpWord\Writer\Word2007`中的函数创建图表。例如,创建一个柱状图: ```php use PhpOffice\PhpWord\SharedDrawing; $data = [ // 数据... ]; $image = $document->addImage('path/to/chart/image.png', 'chart'); $chart = $image->createShape('chart.xlradar') ->setName('My Radar Chart') ->setHeight(200) ->setWidth(300); // 设置图表标题、系列和数据点等属性 $chart->getData() ->setArrayData($data); ``` 5. **将图表插入到文档**:最后将图表插入到Word文档的特定位置。 ```php $section = $document->getActiveSheet()->getCell('A1')->getWorksheet(); $section->addChartObject($chart); ``` 6. **保存文档**:完成所有操作后,使用writer对象将文档保存为`.docx`文件。 ```php use PhpOffice\PhpWord\IOFactory; $writer = \PhpOffice\PhpWord\IOFactory::createWriter($document, 'Word2007'); $writer->save('output.docx'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值