-
Laravel Excel FromView #excel #view #fromView #테이블 #tableLaravel 2023. 4. 30. 22:01반응형SMALL
- export 생성
php artisan make:export VisitExport
@ VisitExport
<?php namespace App\Exports; use Illuminate\Contracts\View\View; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\WithEvents; use Maatwebsite\Excel\Events\AfterSheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; class VisitExport implements FromView, ShouldAutoSize, WithEvents { // view에서 사용할 데이터 protected $items; public function __construct($items) { $index = 1; foreach($items as $item){ $item->index = $index; $index++; } $this->items = $items; } public function view(): View { return view("exports.visits", [ "items" => $this->items ]); } /* 꾸미기 필요할 때 public function registerEvents(): array{ return [ AfterSheet::class => function (AfterSheet $event) { $sheet = $event->getSheet()->getDelegate(); // 기본적으로 $event->getSheet()->getDelegate()는 해당 시트를 접근하기 위한 메소드로 보면 된다. 난 $sheet라는 변수로 할당해서 사용한다. $sheet->getStyle('A:I')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('A2:I2')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); $sheet->getStyle('A3:I3')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); $sheet->getStyle('A:I')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); $sheet->getStyle('A4:I4')->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => '025b94'],]); $sheet->getStyle('A4:I4')->getFont()->getColor()->setARGB("ffffff"); $sheet->getRowDimension(1)->setRowHeight(40); $sheet->getRowDimension(4)->setRowHeight(25); foreach (range('A', 'W') as $columnId) { //A부터 W열까지 AutoSize를 사용하지 않고 내가 직접 입력한 열 크기로 맞춘다. (한글은 autoSize가 안됨.) $sheet->getColumnDimension($columnId)->setAutoSize(false); $sheet->getColumnDimension($columnId)->setWidth(15); } } ]; } */ }
@ views/exports/visits.blade.php
<html> <table> <thead> <tr> <th>구분</th> <th>1억미만</th> <th>비해당</th> <th>무대조명</th> <th>통보장치</th> <th>수용</th> <th>일부수용</th> <th>미수용</th> <th>의견없음</th> <th>합계</th> </tr> </thead> <tbody> @foreach($items as $item) <tr> <td>{{$item["month"]}}월</td> @foreach($item["items"] as $data) <td>{{number_format((float)$data,2,'.','')}}</td> @endforeach <td> {{number_format((float)$item["total"],2,'.','')}} </td> </tr> @endforeach </tbody> </table> </html>
* 주의사항
blade 파일에 head같은곳에 쓸데없는 태그들 다 빼고 깔끔하게 meta, table 정도만 넣어야함
<html> <head> <title>엑셀</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <table> <thead> <tbody> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>캠페인명</strong> </th> <td>[{{$campaign->title_company}}] {{$campaign->title_product}}</td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>신청</strong> </th> <td> {{$campaign->applications()->count()}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>모집</strong> </th> <td> {{$campaign->max_participant}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>선정</strong> </th> <td> {{$campaign->applications()->where("selected", 1)->count()}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>인플루언서 모집기간</strong> </th> <td> {{\Carbon\Carbon::make($campaign->hire_started_at)->format("Y.m.d")}} ~ {{\Carbon\Carbon::make($campaign->hire_finished_at)->format("Y.m.d")}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>인플루언서 선정기간</strong> </th> <td> {{\Carbon\Carbon::make($campaign->select_started_at)->format("Y.m.d")}} ~ {{\Carbon\Carbon::make($campaign->select_finished_at)->format("Y.m.d")}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>인플루언서 작성기간</strong> </th> <td> {{\Carbon\Carbon::make($campaign->review_started_at)->format("Y.m.d")}} ~ {{\Carbon\Carbon::make($campaign->review_finished_at)->format("Y.m.d")}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>등록된 리뷰</strong> </th> <td> {{$campaign->applications()->whereNotNull("url_review")->count()}} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>총 인게이지먼트(누적)</strong> </th> <td> {{ $metrics["count_engagement"] }} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>총 인게이지먼트(평균)</strong> </th> <td> {{ $metrics["average_engagement"] }} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>좋아요수(누적)</strong> </th> <td> {{ $metrics["count_like"] }} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>좋아요수(평균)</strong> </th> <td> {{ $metrics["average_like"] }} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>댓글수(누적)</strong> </th> <td> {{ $metrics["count_comment"] }} </td> </tr> <tr> <th style="width:200px; vertical-align:center; background-color:#ffc800;"> <strong>댓글수(평균)</strong> </th> <td> {{ $metrics["average_comment"] }} </td> </tr> </tbody> </table> </html>
LIST'Laravel' 카테고리의 다른 글
Laravel excel 001 -> 1처럼 0 사라질 때 처리법 (0) 2023.06.09 429 error 안뜨게 하는법 (0) 2023.05.25 초성검색 (ㄱ,ㄴ,ㄷ,ㅁ,ㅂ...) (0) 2023.04.04 유튜브 고유아이디 및 썸네일 url 얻기 #Youtube #유튜브 #썸네 (0) 2023.04.02 Laravel + nuxt 소셜로그인 #sanctum #social #로그인 #소셜 #vue #nuxt #api (0) 2023.03.23