-
엑셀(Excel) 다루기Laravel 2020. 4. 27. 14:12반응형SMALL
# Laravel Excel 패키지를 사용하여 엑셀 다운로드를 진행해볼 예정
https://docs.laravel-excel.com/3.1/getting-started/installation.html
Installation | Laravel Excel
Installation Requirements PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 PHP extension php_zip enabled PHP extension php_xml enabled PHP extension php_gd2 enabled Installation Require this package in the composer.json of your Laravel project. This will downl
docs.laravel-excel.com
1. 세팅
composer require maatwebsite/excel
@ config/app.php
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ] .... 'aliases' => [ ... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ]
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
php artisan make:export CommentsExport --model=Comment
@ CommentExport
<?php namespace App\Exports; use App\Comment; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMapping; class CommentsExport implements FromCollection, WithHeadings, WithMapping { public function collection() { return Comment::all(); } public function map($comment) : array { return [ $comment->url, $comment->name, $comment->body, $comment->published_at ]; } public function headings(): array { return [ "URL", "이름", "내용", "댓글일자" ]; } }
@ CommentController
<?php namespace App\Http\Controllers; use App\Exports\CommentsExport; use Maatwebsite\Excel\Facades\Excel; class CommentsController extends Controller { public function export() { // set_time_limit(0); 이거 걸어두면 10,000 ROW까지는 커버 가능, 근데 그 이상은 chunk 알아봐 return Excel::download(new CommentsExport, 'comments.xlsx'); } }
@ web.php
Route::get('comments/export/', 'CommentsController@export');
LIST'Laravel' 카테고리의 다른 글
Job(Queue) (0) 2020.06.14 ENUM (0) 2020.05.31 이미지 다루기 with 미디어 라이브러리(Media Library) + S3 (0) 2020.04.21 API Resource, Collection (0) 2020.03.27 라라벨 + 리액트 타입스크립트(typescript) (0) 2020.03.26