laravel用phpWord匯出漂亮的word檔案

寞小陌發表於2022-04-01

先記錄一下用表格方式匯出

還有直接佈局、模版檔案替換這種的

<?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 協議》,轉載必須註明作者和本文連結

相關文章