• PHP导出word方法(一phpword)


    背景

    上一篇:MHT导出word文档

    上一篇,已经记录了使用MHT来生成并导出word文档的方法,这次,来记录一下使用php的扩展phpword来生成并导出word;

    使用步骤

    gitlab上composer phpword扩展
    phpword样式使用文档

    • 命令:composer require phpoffice/phpword
      
      • 1

    一、使用方法1(html文件生成word文档)

    但是文档上示例使用并不完全,因为我要导出的word是个简历,有图片,有表格的,所以我想能不能跟MHT一样,先渲染一个html页面,然后导出,还真的有,哈哈哈哈

    <?php
    
    namespace App\Services;
    
    use App\Models\Adresume;
    use PhpOffice\PhpWord\IOFactory;
    use PhpOffice\PhpWord\PhpWord;
    use PhpOffice\PhpWord\Shared\Html;
    use PhpOffice\PhpWord\TemplateProcessor;
    
    
    class WordService
    {
        public function __construct(){
        }
    
    
        public static function downResumeWord($medlive_id,$title='',$updated_at=''){
    
            info('export');
            
            list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
            
            $oUser = Adresume::where('medlive_id',$medlive_id)->first();
    
            $filename = $oUser->truename?$oUser->truename."的简历":"简历";
    
            header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
            header('Content-Disposition: attachment;filename="' . $filename . '.docx"');
            header('Cache-Control: max-age=0');
            
            $phpWord = new PhpWord();
    
            $section = $phpWord->addSection();
    
            $domain = config('app.url');
    
            $html = view('pc.usercenter.resume_doc',compact('domain','oResume','oIntension','oWorkexp','oEduexp','oAttach','title','updated_at'))->render();
    
            Html::addHtml($section, $html,false,false);
    
            $writer = IOFactory::createWriter($phpWord,'Word2007' );
    
            $writer->save('php://output');
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    这里是直接下载下来的,若是你需要保存到服务器的话,如下示例:

    <?php
    
    namespace App\Services;
    
    use App\Models\Adresume;
    use PhpOffice\PhpWord\IOFactory;
    use PhpOffice\PhpWord\PhpWord;
    use PhpOffice\PhpWord\Shared\Html;
    use PhpOffice\PhpWord\TemplateProcessor;
    
    
    class WordService
    {
        public function __construct(){
        }
    
    
        public static function downResumeWord($medlive_id,$title='',$updated_at=''){
    
            info('export');
            
            list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
            
            $oUser = Adresume::where('medlive_id',$medlive_id)->first();
    
            $filename = $oUser->truename?$oUser->truename."的简历":"简历";
    
            $phpWord = new PhpWord();
    
            $section = $phpWord->addSection();
    
            $domain = config('app.url');
    
            $html = view('pc.usercenter.resume_doc',compact('domain','oResume','oIntension','oWorkexp','oEduexp','oAttach','title','updated_at'))->render();
    
            Html::addHtml($section, $html,false,false);
    
            $writer = IOFactory::createWriter($phpWord,'Word2007' );
    
            $writer->save($filename.'docx');
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    二、使用方法2(使用word模板生成word文档)

    不过,该方法并未尝试过,仅供参考哈,给大家找到了一篇细节描述的文章分享
    前人栽树,后人乘凉1:word模板生成word文档

    二、使用方法3(使用phpword属性,自己搭建样式)

    <?php
    
    namespace App\Services;
    
    use App\Models\Adresume;
    use PhpOffice\PhpWord\IOFactory;
    use PhpOffice\PhpWord\PhpWord;
    use PhpOffice\PhpWord\Shared\Html;
    use PhpOffice\PhpWord\TemplateProcessor;
    
    //require_once 'bootstrap.php';
    //require  './vender/autoload.php';
    
    class WordService
    {
        public function __construct(){
        }
    
    
        public static function downResumeWord($medlive_id,$title='',$updated_at=''){
    
    
    
            info('export');
            list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
            $oUser = Adresume::where('medlive_id',$medlive_id)->first();
    
            $filename = $oUser->truename?$oUser->truename."的简历":"简历";
            $phpWord = new PhpWord();
    
    
            /* 方法二:自己一行一行处理样式,可以兼容图片 */
            $fontStyle2 = array('align'=>'center');  //整体样式
            $section = $phpWord->addSection();      //整体页面
            $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 17, 'name' => '宋体'],$fontStyle2);    //设置title样式
    
            $time = date('Y-m-d',time());
            $section->addText("应聘:$title
    			                                              投递时间:$updated_at");  //添加文本
    
            $styleTable = [
                'name' => '微软雅黑',
                'borderSize' => 6,
                'cellMargin' => 20,
            ]; //设置表格样式
    
            $phpWord->addTableStyle('myTable', $styleTable);
            $table = $section->addTable('myTable');
    
    
            //第一行
            $table->addRow(500);
            $table->addCell(5000)
                ->addImage(public_path().$oResume->downthumb, ['gridSpan' => 4,'width'=>350, 'height'=>100, 'valign'=>'center', 'align'=>'center']);
    
    
            //第二行
            $table->addRow(500);
            $table->addCell(5000)
                ->addText($oResume->truename,['size'=>24,'color'=>'1f6990'], ['gridSpan' => 4,'valign'=>'center', 'align'=>'center']);
    
            //第三行
            $table->addRow(500);
            $table->addCell(5000)
                ->addText('>基本信息',['size'=>18,'color'=>'black','bold'=>true], ['gridSpan' => 4,'valign'=>'top', 'align'=>'left']);
    
            //第四行
            $table->addRow(500);
            $table->addCell(5000)
                ->addImage(public_path().'/assets/images/ad/review_line.png', ['gridSpan' => 4,'width'=>540, 'valign'=>'top', 'align'=>'center']);
    
            $writer = IOFactory::createWriter($phpWord,'Word2007' );
            $fileName = $filename.'.doc';
    
            $writer->save( $fileName);
            //如果只是保存到服务器的话到这里就好了
    //        $file = public_path().'/uploadfile/export/'.date('Ymd').'/'  . $fileName;
    //        return response()->download($file); //这里将文件下载下来
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    导出结果如下:
    在这里插入图片描述
    看这,我就累啦,这才一点简历的头部,我就写了那么多代码了,心好累呀,还是直接渲染html页面来的舒服呀

  • 相关阅读:
    面试题:熟悉设计模式吗?谈谈简单工厂模式和策略模式的区别
    一种改进多旋翼无人机动态仿真的模块化仿真环境研究(Matlab代码实现)
    sql 多表 表与表之间的关系,多表查询
    腾讯云CVM服务器数据盘挂载
    沉睡者IT - 10个问题说清楚:什么是元宇宙?
    星环科技向量数据库Transwarp Hippo1.1发布:一库搞定向量+全文联合检索,提升大模型准确率
    JAVA 递归算法- 椰子汁5元一瓶,4个盖子可以换一瓶椰子汁,3个空瓶可以换一瓶椰子汁,那么 100 块钱可以喝多少瓶椰子汁,剩下瓶盖和空瓶各多少?
    实验二 逻辑回归算法实验
    如何在windows环境下编译T
    [java/初学者]static修饰的静态变量、方法及代码块的特性与使用
  • 原文地址:https://blog.csdn.net/snow_love_xia/article/details/125522397