Backend Development 4 min read

Creating Export Classes and Exporting Data with Laravel Excel (Lysice XlsWriter)

This tutorial explains how to generate export classes in Laravel, implement array, query, or collection exports, customize column headers, download files directly, specify file names, formats, response headers, store exports to disk, set document options, and reuse exports with traits, all using the Lysice XlsWriter package.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Creating Export Classes and Exporting Data with Laravel Excel (Lysice XlsWriter)

First, generate an export class with the Artisan command php artisan xls:export XXXXExport array , which creates the class under App\Exports . The class name (e.g., XXXXExport ) determines the export type, supporting array , query , and collection .

To export an array, create a class that implements FromArray and define the array() method returning a two‑dimensional array, as shown below:

<?php
namespace App\Exports;
use Lysice\XlsWriter\Interfaces\FromArray;
class UserExport implements FromArray {
    /** @return array */
    public function array(): array {
        return [
            ['哈哈', 'aaa'],
            ['哈哈', 'aaa'],
            ['哈哈', 'aaa'],
            ['哈哈', 'aaa']
        ];
    }
    /** @return array */
    public function headers(): array {
        return [];
    }
}

If you need column titles, return them from the headers() method.

/**
 * @return array
 */
public function headers(): array {
    return [];
}

For a direct download, define a route that calls Excel::download with a new instance of your export class:

Route::get('xls', function () {
    return Excel::download(new \App\Exports\UserExport());
});

Visiting the xls route triggers the file download.

You can also specify a custom file name:

Route::get('xls', function () {
    return Excel::download(new \App\Exports\UserExport(), 'test');
});

The third argument of Excel::download lets you choose the export format (currently xlsx or csv ), e.g.:

Route::get('xls', function () {
    return Excel::download(new \App\Exports\UserExport(), 'test', \Lysice\XlsWriter\Excel::TYPE_CSV);
});

A fourth argument allows you to set custom response headers:

Route::get('xls', function () {
    return Excel::download(new \App\Exports\UserExport(), 'test', \Lysice\XlsWriter\Excel::TYPE_CSV, ['a' => 'b']);
});

To store the exported file on the server, use Excel::store with the desired path and visibility:

Excel::store((new \App\Exports\UserExport()), 'store.xlsx', 'public', \Lysice\XlsWriter\Excel::TYPE_CSV);

You can also pass additional options such as visibility :

Excel::store((new \App\Exports\UserExport()), 'store.xlsx', 'public', \Lysice\XlsWriter\Excel::TYPE_CSV, ['visibility' => 'private']);

Finally, the Exportable trait can be used in export classes to simplify downloading or storing, for example:

return (new \App\Exports\ExportsExport())->download('2021年3月', \Lysice\XlsWriter\Excel::TYPE_CSV, ['x-download' => true]);

or

(new \App\Exports\ExportsExport())->store('2021年4月', 'public', \Lysice\XlsWriter\Excel::TYPE_XLSX, ['visibility' => 'private']);

These steps provide a complete workflow for exporting data in Laravel using the Lysice XlsWriter package.

backend-developmentPHPExcel ExportLaravelxlswriterLysice
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.