Laravel Excel表格样式不适用于电子表格

时间:2019-01-02 07:51:07

标签: php laravel phpexcel

如何使用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,但是它说

  

您可以使用事件挂接到父包。没必要   如果需要完整的信息,可以使用“查询”或“查看”之类的便捷方法   控制出口。

无论如何,可以直接从表格样式中进行操作吗?谢谢。

2 个答案:

答案 0 :(得分:1)

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

首先,您需要在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);

如果有人能理解我要解释的内容并愿意编辑我的解释,以便其他人更容易理解和理解,我会感到很高兴。 :)

答案 1 :(得分:-1)

            $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);