【问题标题】:How to insert Custom row Laravel excel collection如何插入自定义行 Laravel excel 集合
【发布时间】:2020-11-24 17:14:54
【问题描述】:

我尝试从使用 laravel 的集合中导出 Excel 文件。下面的代码返回给我这个。我需要在列的开头添加 2 个新行,这可能吗?我该怎么办?

<?php

namespace App\Traits;

namespace App\Exports;

// use Illuminate\Support\Collection;

use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

use Maatwebsite\Excel\Concerns\WithCustomStartCell;


class exportSafety implements FromCollection, WithHeadings, WithMapping, WithColumnWidths, WithStyles, WithColumnFormatting, WithCustomStartCell
{
    protected $services;
    protected  $request;

    public function __construct(Collection $services)
    {
        $this->services = $services;
    }

    public function startCell(): string
    {
        return 'A3';
    }

    public function columnWidths(): array
    {
        return [
            'A' => 30,
            'B' => 20,
            'C' => 20,
            'D' => 15,
        ];
    }

    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }
    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            3    => ['font' => ['bold' => true]],
        ];
    }
    public function collection()
    {
        $this->services->each(function ($service) {
            $this->map($service);
        });
        return $this->services;
    }

    public function headings(): array
    {
        $columns = [
            'Name' => 'Name',
            'Data de Nascimento' => 'Data de Nascimento',
            'CPF' => 'CPF',
            'Valor do Seguro' => "Valor do Seguro"
        ];
        return $columns;
    }

    public function startRow(): int
    {
        return 2;
    }

    public function map($service): array
    {
        $columns = [
            'Name' => $service->name,
            'Data de Nascimento' => $service->birthdate,
            'CPF' => $service->cpf,
            'Valor do Seguro' => $service->client->donation_safety
        ];
        return $columns;
    }
}

但我需要这样的东西:

我需要在列的开头放置两个自定义行,这可能吗?我怎么做到的?我正在使用https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html

【问题讨论】:

标签: laravel laravel-excel


【解决方案1】:

你需要使用 WithHeadings trait 来格式化标题行。

$rangeHeadings = [
    'chiqaruvchi',
    'chiqarilgan',
    'sotilgan',
    "sana",
];

public function headings(): array
{
  return [
       [
          $this->product?->name . " product " . $this->params['from_date'] . ' - ' . $this->params['to_date'] . " report",
       ],
       $rangeHeadings,
     ];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多