如何在Laravel Excel中使所有单元格换行并完成所有边框

dgsult0t  于 2023-01-31  发布在  其他
关注(0)|答案(4)|浏览(266)

我想用Laravel Excel导出结果,但结果与我想要的不匹配。

我想要的是

我希望所有的单元格都以文本换行并有完整的边框。
这是我的代码:

class SdgsExportView2 implements FromView, WithTitle {

    protected $id_perangkat_daerah;

    function __construct($id_perangkat_daerah) {
        $this->id_perangkat_daerah = $id_perangkat_daerah;
    }

    public function view(): View
    {
        $data_sdgs = DataSdgs::where('id_perangkat_daerah',$this->id_perangkat_daerah)
        ->with('indikator.target.sdgs')
        ->with('detail_data_sdgs.kegiatan.program.perangkat_daerah')
        ->get();
        return view('template.exportMatrik2', [
            'data_sdgs' => $data_sdgs
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:W100'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
                $event->sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setWrapText(true);
            }
        ];
    }

    public function title() : string
    {
        return 'MATRIK 2';
    }
}

我真的希望你能帮忙

eoxn13cs

eoxn13cs1#

我知道现在回答有点晚(8个月后),但是在代码中添加边框格式相对简单。在事件函数AfterSheet::class => function(AfterSheet $event)中,您需要添加以下代码:

$styleHeader = [
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => 'thin',
                        'color' => ['rgb' => '808080']
                    ],
                ]
            ];
    $event->sheet->getStyle("A1:C1")->applyFromArray($styleHeader);

并将“A1:C1”替换为标题范围。

pod7payv

pod7payv2#

对于我来说,我通过扩展WithEvents添加了事件,并使用

use Maatwebsite\Excel\Concerns\WithEvents;

然后我这样涂...

public function registerEvents(): array
{
    $alphabetRange  = range('A', 'Z');
    $alphabet       = $alphabetRange[$this->totalValue+6]; // returns Alphabet

    $totalRow       = (count($this->attributeSets) * 3) + count($this->allItems)+1;
    $cellRange      = 'A1:'.$alphabet.$totalRow;

    return [
        AfterSheet::class    => function(AfterSheet $event) use($cellRange) {
            $event->sheet->getStyle($cellRange)->applyFromArray([
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                        'color' => ['argb' => '000000'],
                    ],
                ],
            ])->getAlignment()->setWrapText(true);
        },
    ];
}

欲了解详情或更多信息,您可以按照文档https://docs.laravel-excel.com/3.1/getting-started/

pu82cl6c

pu82cl6c3#

@Zahid回答v3.1的一点点更新

public function registerEvents(): array
        {
            return [
                AfterSheet::class    => function(AfterSheet $event) {

                    $alphabet       = $event->sheet->getHighestDataColumn();
                    $totalRow       = $event->sheet->getHighestDataRow();
                    $cellRange      = 'A7:'.$alphabet.$totalRow;

                    $event->sheet->getStyle($cellRange)->applyFromArray([
                        'borders' => [
                            'allBorders' => [
                                'borderStyle' => Border::BORDER_HAIR,
                            ],
                        ],
                    ])->getAlignment()->setWrapText(true);

                },
            ];
        }
mzillmmw

mzillmmw4#

我认为这可能会帮助您找到所需的内容:phpspreadsheet simpler

有一种方法

// $titleName : pass data that you wants to set (not array)
// $mergeTill : pass count of array dataset (Int)

public function setTitle($titleName, $mergeTill, $startCell ,$styleName = "")

在给定link的情况下完成此示例

相关问题