Customized website Package in Project Laravel

Tram Ho

1. Introduction

In any management project, it is indispensable to data statistics by exporting / importing data. Today I would like to introduce to you the Maatwebsite / Laravel-Excel package – a package that allows us to easily export / import data, with many more utilities.

First, let’s install the package Let’s GO ^^ In this article, I use the latest version 3.1.

2. Installation

2.1 Supported Versions

VersionLaravel VersionPhp Version 3support
2.1<= 5.6<= 7.0Unsupported since 15-5-2018
3.0^ 5.5^ 7.0Unsupported since 31-12-2018
3.1^ 5.5, ^ 6.0, ^ 7.0, ^ 8.0^ 7.2New features

2.2 Package installation

Very simply, we will install the package via composer with just one command:

Wait a minute, after the package is installed, I can now use it.

3. Usage

Ummmmm … I will skip the steps of creating project laravel, migrate, seed data, … okay! Here I have created an export-project project and used the available table users. Maatwebsite / Laravel-Exce package supports many types of file extension such as: * .csv, * .xls, * .html, … it’s really convenient, right.

First I will create a file export app / Exports / UsersExport.php

Then in web.php I have defined a route:

And the last step 1 creates an ExportController:

In this Controller file, I will write an export function () with the following content:

Here you can freely get data according to the conditions you want. In addition to this way, you also may not need to process data on the Controller side but can handle the date on the UsersExport file in the collection () function. Now I go to UsersExport file to edit a bit.

Then, access http: // localhost: 8000 / export and you can download a users.xlsx file to your computer. However, this file I have not had any format, so it is quite ugly

Part 4 of this I will guide you to some formats to make your files look more sparkling.

4. Some file formats you may not know

4.1 Format data

In addition to how in the UsersExport file implements FromCollection we can implements from FromArray . For example, here I do not want to get the timestamp column users.created_at, but I want to format it in a different format that looks good, I can use the function array () to reformat them mask:

You can format to add / remove any here. Each $ item corresponds to the values ​​of 1 row.

4.2 Format heading

A file cannot lack the heading, right. Similar to the above here, UsersExport, I have additional implements WithHeadings . Ting Ting, I will add headings for users.xlsx file in the order of each column I just formatted above.

4.3 Set StartCell

Implements WithCustomStartCell :

4.4 Set Title

Implements WithTitle :

4.5 Format Events

Implements WithEvents Now we will go to the font format, size, with, color, …

And the result now I have a beautiful page like this nè!

Conclude

The above article I have shared with you how to use the Maatwebsite / Laravel-Excel package to serve the export of files in a laravel project.

To learn more about the package, you can refer to the package homepage! Thanks mn for reading my article

https://docs.laravel-excel.com/3.1/getting-started/ https://github.com/Maatwebsite/Laravel-Excel

Share the news now

Source : Viblo