Import and export data in Laravel with Maatwebsite / Excel package

Tram Ho

I. Introduction

Nowadays, when making a web application, many of you have thought of importing and exporting data into a database through files such as excel, csv (importing product lists, articles, users …) because it helps us easily add and retrieve data from the site. In today’s article I will introduce to you the Maatwebsite/Laravel-Excel package, which can meet all the needs mentioned above with a lot of features included.

The version I use will be version 3.1 and below, I will guide you based on small examples, to help you easily imagine in learning about the package. Let’s get started ?

II.Installation

1.Requirements

  • PHP: ^ 7.0
  • Laravel: ^ 5.5
  • PhpSpreadsheet: ^ 1.6
  • PHP extension: php_zip, php_xml, php_gd2
VersionLaravel VersionPhp Versionsupport
2.1<= 5.6<= 7.0Unsupported since May 15, 2018
3.0^ 5.5^ 7.0Unsupported since 12/31-2018
3.1^ 5.5 | ^ 6.0^ 7.1New features

2.Install the package

First, type this command into the terminal

Add provider and alias to config/app.php file

The next thing is to publish the configuration file of the package with the command

The above command will create the config file excel.php into the config directory and it is also the final step to install the Maatwebsite/Laravel-Excel package, now we move on to the next section to learn the cool features. This package is for us only ?

III.Learn about export data export

For import / export data into an Excel file, the Maatwebsite/Laravel-Excel package provides us with many features such as exporting files with multiple formats, exporting format, mapping data, exporting files via view blade, etc. But in this article I just listed some basic functions that I use and I think enough to meet your needs <33

1. Export a file

The first is we will create the appExports folder

Next, type the following command in terminal:

After running the above command will create a file in the appExport folder named UserExport.php , you fix that file with the following content:

In the appHttpControllersUserController.php file appHttpControllersUserController.php we add the export() function

You can change the file extension in users.xlsx to get the desired file format such as .xls , .csv , .ods , .html , …

In routesweb.php :

Thus, after entering the user/export , we will receive a file named users.xlsx created above. In the file received above, there is information about all fields of all user currently in the database.

No.account nameEmailSpecies
first17020907[email protected]0
217020908[email protected]0
317020909[email protected]0

2. Get the data according to conditions

In the above part, I have instructed to export to a file containing all users, now we will export a file containing only information of one user or several users.

In the UserExport.php file, we add the __construct() function:

In UserController.php we fix the following:

In routesweb.php :

So you can export the file containing the user according to the given id, with other conditions you do the same steps above.

3. Export file based on laravel view

As I said, this package provides us with a lot of useful features, including the export feature based on the .blade.php view. Personally, I think this feature makes it easier for us to create our own custom tables than above.

In the UsersExport.php file, you implements FromView :

Create users.blade.php file containing the information of the user , the package will automatically convert the html table into excel table:

In the file UserController.php you modify the export() function export()

4. Create sheet

You probably all know Excel’s sheet , which are tabs that help us manage and manipulate data more easily. Below I will guide you to divide the sheet through the example of taking accounts for each month of participation and then dividing into sheet .

In the UsersExport.php file

Create UsersPerMonthSheet.php file, you can implements FromQuery , FromCollection , FromView

Add the export () function in the UserController.php controller:

IV. Learn about the data import feature import

1. Import a file

The first is we will create the appImports folder

Create the import file by typing the following command into the terminal:

In the UsersImport.php file:

Above, the headingRow() function aims to remove the table row headers, the function will return the number of rows you ignore, as in the above example, I will ignore the row containing the column’s information such as the name. account, email, password … Package allows us to use the keywords in the header to lock the array of each row. For example, I entered data for the username attribute based on the username column or account name.

Note:

  • If in your Excel file the column name is “Account name” then the import file will be $row['ten_tai_khoan']
  • If you enter data in the format of dates from the Excel file and encounter errors, try to solve this way, I have been and fix errors quite effectively. ?

In UserController.php :

In user.blade.php file:

In routesweb.php :

2. Match inserts

When you import a large file, it means that a large number of rows will be imported from there, resulting in bottlenecks or bottlenecks, which will affect data entry. By implements WithBatchInserts we will solve that problem by deciding the number of lines that will be entered into the database each time.

3.Chunk reading

Unlike the above, chunk reading will determine the number of lines that will be read each time instead of reading all and then temporarily stored in memory, thereby reducing memory usage, helping to better control memory.

Above are a few basic and useful features that the Maatwebsite/Laravel-Excel package has brought, helping a lot in importing and exporting data with Excel. If you want to learn more about other features, please read the docs of Maatwebsite link below.

Hopefully this article will be of some help to you in working with files such as excel, ods, csv … Thank you for patiently reading until the end of the article. ?

References

https://docs.laravel-excel.com/3.1

Share the news now

Source : Viblo