laravel用phpWord导出漂亮的word文件

先记录一下用表格方式导出

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

<?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); //这里将文件下载下来
    }
}

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

有实现单元格跨行操作的小伙伴欢迎留言呀 :see_no_evil:

1年前 评论

用预定义的模板

1年前 评论
寞小陌 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!