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

问题描述 投票:1回答:1

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

我已经有了我想要的观点:

<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>

这就是它在浏览器上的样子

enter image description here

但这就是我对excel的看法

enter image description here

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

您可以使用事件挂钩到父包。如果您需要完全控制导出,则无需使用“查询”或“查看”等便捷方法。

无论如何直接从表格样式做到这一点?谢谢。

php laravel phpexcel
1个回答
2
投票

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

首先,您需要在PHPExcelMacroServiceProvider中创建一个名为App\Providers\的新提供程序,您将放置生成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找到你想要的属性,你只需要搜索你想要的功能或格式,然后按照这个规则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);

如果有人理解我想要解释的内容并且愿意编辑我的解释,我会很高兴,所以我的答案更易读,更容易理解。 :)

© www.soinside.com 2019 - 2024. All rights reserved.