【问题标题】:How can I set text align right in the column on the laravel Excel maatwebsite?如何在 laravel Excel maatwebsite 的列中设置文本对齐?
【发布时间】:2018-08-07 18:23:50
【问题描述】:

我从这里得到参考:https://laravel-excel.maatwebsite.nl/3.0/getting-started/

我一直在寻找如何设置文本对齐,但在文档中没有找到

我的脚本导出如下:

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class InvoiceExport implements FromView
{
    use Exportable;
    public function view(): View
    {
        $data = Invoice::get();
        return view('exports.item', [
            'data' => $data
        ]);
    }
}

我该如何解决这个问题?

更新

我找到了解决方案,但并不完美

public function registerEvents(): array
{
    return [
        AfterSheet::class    => function(AfterSheet $event) {
            $event->sheet->styleCells(
                'C2:C1000',
                [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                    ],
                ]
            );
        },
    ];
}

它有效。但我的记录是动态的。它可以是 1000 条记录。可以是10000条记录

在我上面的脚本中,它只是阻止从 C2 到 C1000。我想在 C 列中设置所有记录

我该怎么做?

【问题讨论】:

  • 也许这可以回答您的问题,请参阅:github.com/Maatwebsite/Laravel-Excel/issues/1597
  • @Oluwatobi Samuel Omisakin 我使用 laravel excel maatwebsite 版本 3。没有版本 2
  • 不幸的是,我不确定 3.0 是否具有该功能,但我可能错了。也可以在 Github 上查看一些熟悉的问题:github.com/Maatwebsite/Laravel-Excel/…
  • @OluwatobiSamuelOmisakin 我已经看到了,但我没有找到解决方案
  • 在您的 exports.item 刀片文件中,只需在需要右对齐 Excel 单元格的位置使用 &lt;td style="text-align: right;"&gt;...&lt;/td&gt;(或 &lt;td align="right"...)。这是针对 2.1 版的,但我确信逻辑仍然适用:laravel-excel.maatwebsite.nl/2.1/blade/…

标签: excel laravel laravel-5 laravel-5.6 laravel-excel


【解决方案1】:

只需使用 C:C 即可选择整个列:

'C:C',
[
    'alignment' => [
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
    ],
]

【讨论】:

    【解决方案2】:

    对于 Laravel Excel 3.1.12,我们可以使用 registerEventsMacro

    ...
    
        public function registerEvents(): array
        {
            return [
                // array callable, refering to a static method.
                AfterSheet::class => [self::class, 'afterSheet'],
            ];
        }
    
        public static function afterSheet(AfterSheet $event)
        {
            Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
                $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
            });
    
            $event->sheet->styleCells('D:D', [
                'alignment' => [
                    'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                ],
            ]);
        }
    
    ...
    

    【讨论】:

      【解决方案3】:

      下面的代码对我有用

       $event->sheet->getStyle('A1:B48')->getAlignment()->setHorizontal('center');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2018-06-23
        • 2019-01-28
        • 1970-01-01
        • 2017-11-12
        • 2019-05-20
        • 1970-01-01
        相关资源
        最近更新 更多