Laravel Excel table 样式未应用于电子表格

Laravel Excel table styles not applied to spreadsheet

如何使用 Laravel Excel 中的 table 样式制作电子表格?

我已经有了我想要的视图:

<table border="1">
    <thead>
        <tr>
            <td>Tema/Sub Tema/Sub-sub Tema:</td>
            <td></td>
        </tr>
        <tr>
            <td>Kelompok/Usia:</td>
            <td>{{ $group->name }} / {{ $group->age_range }} tahun</td>
        </tr>
    </thead>
</table>

<table border="1">
    <thead>
        <tr>
            <td rowspan="4">No</td>
            <td rowspan="4">Nama Anak</td>
            @foreach ($fod_indicators as $fod_indicator)
            <td colspan="4">&nbsp;</td>
            @endforeach
            <td colspan="4">ID</td>
        </tr>
        <tr>
            @foreach ($fod_indicators as $fod_indicator)
            <td colspan="4">
                <b>{{ $fod_indicator->fod->name }}</b> <br />
                {{ $fod_indicator->indicator->name }}
            </td>
            @endforeach
            <td>CP / STTPA</td>
        </tr>
        <tr>
            @foreach ($fod_indicators as $fod_indicator)
            <td colspan="4">{{ $fod_indicator->indicator->assessment_technique }}</td>
            @endforeach
            <td>Teknik Penilaian</td>
        </tr>
        <tr>
            @foreach ($fod_indicators as $fod_indicator)
            <td>BB</td>
            <td>MB</td>
            <td>BSH</td>
            <td>BSB</td>
            @endforeach
            <td>Keterangan</td>
        </tr>
    </thead>
    <tbody>
        @foreach ($scores as $score)
        <tr>
            <td align="center">{{ $loop->iteration }}</td>
            <td>{{ $score->child->full_name }}</td>
            @foreach ((object) json_decode($score->scores) as $indicator_star)
                @for ($star = 1; $star <= 4; $star ++)
                <td width="40" align="center">{!! $indicator_star->stars == $star ? '&#10004;' : '&nbsp;' !!}</td>
                @endfor
            @endforeach
        </tr>
        @endforeach
    </tbody>
</table>

<table>
    <tfoot>
        <tr>
            <td>Catatan Anekdot:</td>
        </tr>
        <tr>
            <td>{{ $report->notes }}</td>
        </tr>
    </tfoot>
</table>

这是它在浏览器上的样子

但这就是我在 excel

上得到的

我已经查看了该网站,但没有 table 视图的文档,我想使用 https://laravel-excel.maatwebsite.nl/3.1/exports/extending.html#customize,但它说

You are able to hook into the parent package by using events. No need to use convenience methods like "query" or "view", if you need full control over the export.

有没有直接从 table 样式做的?谢谢。

我想我早就应该在这里分享我的解决方案,但如果我的解释不够好,我很抱歉,希望你能理解我在这里试图解释的内容。

首先,您需要在 App\Providers\ 中创建一个名为 PHPExcelMacroServiceProvider 的新提供程序,您将在其中放置制作 excel 文件所需的所有宏,这就是它在我的提供商文件中看起来像:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
Use \Maatwebsite\Excel\Sheet;
use \Maatwebsite\Excel\Writer;

class PHPExcelMacroServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        /**
         * Page format macros
         */
        Writer::macro('setCreator', function (Writer $writer, string $creator) {
            $writer->getDelegate()->getProperties()->setCreator($creator);
        });

        Sheet::macro('setOrientation', function (Sheet $sheet, $orientation) {
            $sheet->getDelegate()->getPageSetup()->setOrientation($orientation);
        });

        /**
         * Cell macros
         */
        Writer::macro('setCellValue', function (Writer $writer, string $cell, string $data) {
            $writer->getDelegate()->setCellValue($cell, $data);
        });

        Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
            $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
        });

        Sheet::macro('horizontalAlign', function (Sheet $sheet, string $cellRange, string $align) {
            $sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setHorizontal($align);
        });

        Sheet::macro('verticalAlign', function (Sheet $sheet, string $cellRange, string $align) {
            $sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setVertical($align);
        });

        Sheet::macro('wrapText', function (Sheet $sheet, string $cellRange) {
            $sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setWrapText(true);
        });

        Sheet::macro('mergeCells', function (Sheet $sheet, string $cellRange) {
            $sheet->getDelegate()->mergeCells($cellRange);
        });

        Sheet::macro('columnWidth', function (Sheet $sheet, string $column, float $width) {
            $sheet->getDelegate()->getColumnDimension($column)->setWidth($width);
        });

        Sheet::macro('rowHeight', function (Sheet $sheet, string $row, float $height) {
            $sheet->getDelegate()->getRowDimension($row)->setRowHeight($height);
        });

        Sheet::macro('setFontFamily', function (Sheet $sheet, string $cellRange, string $font) {
            $sheet->getDelegate()->getStyle($cellRange)->getFont()->setName($font);
        });

        Sheet::macro('setFontSize', function (Sheet $sheet, string $cellRange, float $size) {
            $sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize($size);
        });

        Sheet::macro('textRotation', function (Sheet $sheet, string $cellRange, int $degrees) {
            $sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setTextRotation($degrees);
        });

    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

您可以从 here, you just need to search what function or format you want, and then add it to the provider file with following this rule https://docs.laravel-excel.com/3.1/imports/extending.html#sheet.

中找到您想要的属性

之后,确保将 PHPExcelMacroServiceProvider 添加到 config/app.php

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    ... // Bunch of providers

    App\Providers\PHPExcelMacroServiceProvider::class, // Add this provider to the list

],

所以要使用您刚刚在 app/Exports/ExampleReportExport.php 中制作的宏,我只需要调用宏即可。例如:

$event->sheet->wrapText('A1:AC100');
$event->sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$event->sheet->setFontFamily('A1:AC100', 'Times New Roman');
$event->sheet->horizontalAlign('D1:Q2' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E6:X6' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->verticalAlign('A1:AC100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A6:X7', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E8:AC100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); 
$event->sheet->verticalAlign('E7:X7' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A8:D100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT); 
$event->sheet->textRotation('E7:X7', 90);

如果有人理解我要解释的内容并愿意编辑我的解释,以便我的回答对其他人来说更易读和更容易理解,我将很高兴。 :)

            $styleArray = [
                'font' => [
                    'bold' => true,
                ],
                'alignment' => [
                    'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
                ],
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    ],

                ],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
                    'rotation' => 90,
                    'startColor' => [
                        'argb' => 'FFA0A0A0',
                    ],
                    'endColor' => [
                        'argb' => 'FFFFFFFF',
                    ],
                ],
            ];

            $to = $event->sheet->getDelegate()->getHighestColumn();
            $event->sheet->getDelegate()->getStyle('A1:'.$to.'1')->applyFromArray($styleArray);