13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel Excel 3.1 レイアウトまとめ

Last updated at Posted at 2019-07-01

概要

Laravel Excel 3.1で設定できるレイアウトのまとめです.
シート全体に対して適用されます.
ビューを使用時に各セルごとで設定できるスタイルはここにまとめています.

環境

  • Laravel 5.8
  • Laravel Excel 3.1

実装

以下はすべて

php artisan make:export ModelExport --model=Model

で作成したファイルの

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeSheet;

class BookExport implements FromCollection , WithEvents
{
    public function registerEvents() : array
    {
        return [
            //ここに追加する
        ];
    }
}

return [
    AfterSheet::class => function(AfterSheet $event) {
        //処理
    },
];

このように追加します.キーが同じものは処理をまとめてください.

印刷の向き

use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;

AfterSheet::class => function(AfterSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $sheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
}

PageSetup::XXX の部分は

ORIENTATION_DEFAULT = 'default';  //デフォルト
ORIENTATION_LANDSCAPE = 'landscape';  //横
ORIENTATION_PORTRAIT = 'portrait';  //縦

から選びます.

目盛線の有無


AfterSheet::class => function(AfterSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $sheet->setShowGridlines(false);  //trueで表示する
}

フォント

BeforeSheet::class => function(BeforeSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $sheet->getParent()->getDefaultStyle()->applyFromArray([
        'font' => [
            'name' =>  'MS Pゴシック',
            'size' => 10,
        ],
    ]);
},

設定できる項目は

'font' => [
    'name' => 'フォント名',  //フォント
    'bold' => true,  //ボールド体
    'italic' => true,  //イタリック体
    'superscript' => true,  //上付き文字
    'subscript' => false,  //下付き文字
    'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,  //下線
    'strikethrough' => true,  //取り消し線
    'color' => [
        'rgb' => 'FF0000'  //色
    ],
    'size' => 12,  //文字サイズ
],

これだけあります.

セルの幅

BeforeSheet::class => function(BeforeSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $width = [
        'A' => 10,
        'B' => 12,
        'C' => 11,
    ];
    // Disable the autosize and set column width
    foreach ($width as $column => $value) {
        $sheet->getColumnDimension($column)
            ->setAutoSize(false)
            ->setWidth($value);
    }
    // Set autosized to true
    $sheet->hasFixedSizeColumns = true;
},

セルの高さ

BeforeSheet::class => function(BeforeSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $sheet->getDefaultRowDimension()->setRowHeight(20);
},

配置

use PhpOffice\PhpSpreadsheet\Style\Alignment;

BeforeSheet::class => function(BeforeSheet $event) {
    $sheet = $event->getSheet()->getDelegate();
    $sheet->getParent()->getDefaultStyle()->applyFromArray([
        'alignment' => [
            'horizontal' => Alignment::HORIZONTAL_CENTER,  //水平
            'vertical' => Alignment::VERTICAL_CENTER,  //垂直
            'textRotation' => 0,  //回転
            'wrapText' => FALSE,  //折返し
        ],
    ]);
}

配置は水平方向は

Alignment::XXXX 配置
HORIZONTAL_GENERAL 通常
HORIZONTAL_LEFT 左寄せ
HORIZONTAL_RIGHT 右寄せ
HORIZONTAL_CENTER 中央揃え
HORIZONTAL_CENTER_CONTINUOUS 選択範囲で中央揃え
HORIZONTAL_JUSTIFY 両端揃え
HORIZONTAL_FILL 繰り返し

から選び,垂直方向は

Alignment::XXXX 配置
VERTICAL_BOTTOM 下揃え
VERTICAL_TOP 上揃え
VERTICAL_CENTER 中央揃え
VERTICAL_JUSTIFY 両端揃え

から選びます.

その他

その他 applyFromArray メソッドで適用できるスタイルに

  • fill
  • borders
  • numberformat
  • protection
  • quotePrefix

があります(調査中).

参考

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?