laravel用phpWord导出漂亮的word文件

Laravel框架
467
0
0
2022-09-21

先安装下phpWord扩展包

composer require phpoffice/phpword

git库地址

https://github.com/PHPOffice/PHPWord

记录一下用表格方式导出

还有直接布局、模版文件替换这种的

<?php

namespace App\Exports;

use App\Models\User;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;

class UserInfoExport
{
    protected $user;

    public static function export(User $user)
    {
        $phpWord = new PhpWord();

        $fontStyle = ['align' => 'center'];
        //整体页面    
        $section = $phpWord->addSection();

        //标题样式    
        $phpWord->addTitleStyle(1, [
            'bold' => true,
            'color' => 000,
            'size' => 20,
            'name' => '宋体',
        ], $fontStyle);
        $section->addTitle('用户资料--' . $user->name);
        $section->addTextBreak(2);//2个换行

        //预定义样式    
        $cellRowSpan = ['vMerge' => 'restart', 'valign' => 'center']; //设置可跨行,且文字居中    
        $cellRowContinue = ['vMerge' => 'continue'];//使行连接,且无边框线    
        $cellColSpan2 = ['gridSpan' => 2, 'valign' => 'center'];//设置跨列    
        $cellColSpan4 = ['gridSpan' => 4, 'valign' => 'center'];//设置跨列    
        $cellHCenter = ['align' => 'center'];
        $cellLeft = ['align' => 'left'];
        $cellVCenter = ['valign' => 'center'];

        $styleFont = ['name' => '宋体', 'size' => 14];
        $imageStyle = ['width' => 130, 'height' => 130, 'align' => 'center'];

        $table = $section->addTable(['borderColor' => '666666', 'borderSize' => 6, 'cellMargin' => 50]);
        $table->addRow(500);//添加一行,500twip(缇)   500/15 好像是像素值 自己百度吧    
        $table->addCell(15000, $cellVCenter)->addText('姓名', $styleFont, $cellHCenter);//第1列    
        $table->addCell(5000, $cellVCenter)->addText($user->name?? $user->nickname, $styleFont, $cellHCenter);//第2列    
        $table->addCell(10000, $cellVCenter)->addText('性别', $styleFont, $cellHCenter);//第3列    
        $genderName = $user->gender == 1 ? '男' : ($user->gender == 0 ? '女' : '未知');
        $table->addCell(5000, $cellVCenter)->addText($genderName, $styleFont, $cellHCenter);//第4列    
       //网络图片不行的话先下载到本地,用相对路径,用完再删除    
        $table->addCell(5000, $cellRowSpan)->addImage($user->img, $imageStyle);

        $table->addRow(500);//第二行    
        $table->addCell(5000, $cellVCenter)->addText('手机号', $styleFont, $cellHCenter);
        $table->addCell(5000, $cellVCenter)->addText($user->mobile, $styleFont, $cellHCenter);
        $table->addCell(5000, $cellVCenter)->addText('身份证', $styleFont, $cellHCenter);
        $table->addCell(5000, $cellColSpan2)->addText($user->id_card, $styleFont, $cellHCenter);

        $table->addRow(500);
        $table->addCell(5000, $cellVCenter)->addText('兴趣爱好', $styleFont, $cellHCenter);
        $table->addCell(5000, $cellColSpan4)->addText($user->hobby, $styleFont, $cellLeft);

        $table->addRow(500);
        $table->addCell(5000, $cellVCenter)->addText('参加过的活动', $styleFont, $cellHCenter);
        $cell = $table->addCell(5000, $cellColSpan4);
        foreach ($user->activity_apply as $apply){
            //列表,样式懒得去调了,太费时间了    
            $cell->addListItem($apply->activity->name.'----'.$apply->created_at->toDateString(),0,$styleFont, $cellLeft);
            $cell->addListItem('【评论】'.$apply->comment,0,$styleFont, $cellLeft);
        }

        $writer = IOFactory::createWriter($phpWord, 'Word2007');
        $fileName = '用户资料--' . $user->name . '.docx';
        $writer->save('./export/' . $fileName);
        //如果只是保存到服务器的话到这里就好了    
        $file = public_path('/export/') . $fileName;
        return response()->download($file); //这里将文件下载下来
    }
}

暂时还没有实现单元格跨行,所以第一行因为图片的缘故,会导致有点长,有解决方案的小伙伴欢迎留言。