首页  >  文章  >  后端开发  >  PHP制作word简历

PHP制作word简历

不言
不言原创
2018-04-03 15:59:355386浏览

PHP操作word有一个非常好用的轮子,就是phpword,该轮子可以在github上查找到(PHPOffice/PHPWord)。上面有较为详细的例子和代码,其中里面的源码包含有一些常用的操作例子,包括设置页眉、页脚、页码、字体样式、表格、插入图片等常用的操作。这里介绍的是如何使用该轮子来制作一个简历。

在许多招聘网站都有一个简历下载的功能,如何用php实现呢?在PHPOffice/PHPWord里面就有一个非常简单的生成一个word文档,向文档中插入一些文字。这里我使用的方式比较取巧,这个轮子的说明文档中有template processing,我理解为模板替换,也就是跟laravel的blade模板一个概念。接下来就不多废话,直接说如何操作,这里提一句使用的是laravel框架。

1.安装PHPOffice/PHPWord

composer require phpoffice/phpword

2.创建控制器DocController及test方法用于测试,并建立路由。

php artisan make:controller DocController

3.建立word模板,这里说明一下,该轮子替换的是word文档中格式为${value}格式的字符串,这里我简易的搭建一个模板如下图1所示:
PHP制作word简历
从图中可以看到有一些基本的信息,这些可以从数据库中捞取数据。不过这次是直接使用替换的方式,像工作经历和教育经历这种多行表格的模式这里也只需要取一行作为模板即可。

4.具体代码

//load template docx
        $templateProcessor = new TemplateProcessor('./sample.docx');

        //基础信息填写替换
        $templateProcessor->setValue('update_at', date('Y-m-d H:i:s'));
        $templateProcessor->setValue('number', '123456');
        $templateProcessor->setValue('Name', '张三');
        $templateProcessor->setValue('sex', '男');
        $templateProcessor->setValue('birth', '1996年10月');
        $templateProcessor->setValue('age', '22');
        $templateProcessor->setValue('shortcut', '待业/aaa');
        $templateProcessor->setValue('liveArea', '福建省莆田市涵江区');
        $templateProcessor->setValue('domicile', '福建省莆田市涵江区');
        $templateProcessor->setValue('address', '');
        $templateProcessor->setValue('hopetodo', 'IT');
        $templateProcessor->setValue('hopeworkin', '互联网');
        $templateProcessor->setValue('hopes', '7000+');
        $templateProcessor->setValue('worklocation', '福建省莆田市');
        $templateProcessor->setValue('phone', '123456789');
        $templateProcessor->setValue('mail', '456789@qq.com');
        $templateProcessor->setValue('qqnum', '456789');
        $templateProcessor->setValue('selfjudge', '哇哈哈哈哈哈哈哈');

        //工作经历表格替换
        $templateProcessor->cloneRow('experience_time', 2);//该表通过克隆行的方式,形成两行
        $templateProcessor->setValue('experience_time#1', '2010-09~2014-06');//每行参数是用value#X(X表示行号,从1开始)
        $templateProcessor->setValue('job#1', 'ABC company CTO');
        $templateProcessor->setValue('experience_time#2', '2014-09~至今');
        $templateProcessor->setValue('job#2', 'JBC company CTO');

        //教育经历
        $templateProcessor->cloneRow('time', 2);
        $templateProcessor->setValue('time#1', '2010-09~2014-06');
        $templateProcessor->setValue('school#1', 'ABC');
        $templateProcessor->setValue('major#1', 'Computer science');
        $templateProcessor->setValue('time#2', '2014-09~至今');
        $templateProcessor->setValue('school#2', 'JBC');
        $templateProcessor->setValue('major#2', 'Computer science');

        //语言能力
        $templateProcessor->cloneRow('lang',2);
        $templateProcessor->setValue('lang#1', '汉语|精通');
        $templateProcessor->setValue('lang#2', '英语|精通');

        //技能
        $templateProcessor->cloneRow('skill',3);
        $templateProcessor->setValue('skill#1', 'JAVA|精通');
        $templateProcessor->setValue('skill#2', 'Python|精通');
        $templateProcessor->setValue('skill#3', 'PHP|精通');

        // Saving the document
        $templateProcessor->saveAs('my.docx');

这样就可以通过建立word模板的方式产生一个简历了。

相关推荐:

php制作圆形用户头像方法

PHP制作毫秒时间戳的方法

如何使用Thinkphp制作网页直播




以上是PHP制作word简历 的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn