Laravel-excel: [์งˆ๋ฌธ] - ๊ธฐ๋ณธ ์‹œํŠธ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2018๋…„ 03์›” 26์ผ  ยท  2์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Maatwebsite/Laravel-Excel

๋ฒ„์ „

  • PHP ๋ฒ„์ „: 7.1.9
  • ๋ผ๋ผ๋ฒจ ๋ฒ„์ „: 5.5
  • ํŒจํ‚ค์ง€ ๋ฒ„์ „: 3.0

์„ค๋ช…

๋ฐ์ดํ„ฐ๋ฅผ Excel ํŒŒ์ผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ๋ฌธ์„œ์—์„œ ์‹œํŠธ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
Worksheet๋ฅผ ๊ธฐ๋ณธ ์‹œํŠธ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ์ œ๊ณตํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

์ด ํ›Œ๋ฅญํ•œ ํŒจํ‚ค์ง€์— ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๋‚ด ์ฝ”๋“œ

/** Voucher Export Class **/
namespace Modules\VoucherManagement\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\Voucher;

class VouchersExport implements FromView
{    
    /**
     * <strong i="15">@return</strong> View
     */
    public function view(): View
    {
        $vouchers = Voucher::getAllVouchers();

        return view('templates.export', [
            'vouchers' => $vouchers
        ]);
    }
}
/*** End of Voucher Export Class ***/

/*** Voucher Controller ***/
public function export(Request $request, Excel $excel, VouchersExport $export)
{        
     return $excel->download($export, 'vouchers.xlsx');
}
/*** End of Voucher Controller ***/

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

WithTitle Class๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋‹ต์„ ์–ป์—ˆ๊ณ  ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

use Maatwebsite\Excel\Concerns\WithTitle;
...

class VouchersExport implements FromView, WithTitle {
    ...
    public function title(): string
    {
        return 'Vouchers';
    }
}

๋ชจ๋“  2 ๋Œ“๊ธ€

WithTitle Class๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋‹ต์„ ์–ป์—ˆ๊ณ  ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

use Maatwebsite\Excel\Concerns\WithTitle;
...

class VouchersExport implements FromView, WithTitle {
    ...
    public function title(): string
    {
        return 'Vouchers';
    }
}

@praditha ๋Š” ๋‚ด๊ฐ€ ํ•„์š”๋กœ ํ–ˆ๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰