export excel file with laravel-excel 3.1

Tram Ho

I. The Problem.

Working with files is not uncommon for laravel projects. Laravel-excel, also known as maatwebsite package, is too close to those who need to manipulate files. With useful functions such as:

With the latest 3.1 version, for me it can be considered difficult to use because the doc is written quite short, the functions I need are almost not mentioned. I don’t know whether it was accidental or intentional, but the older versions I found were quite detailed writing and had what I needed like auto spacing columns, so I just did exporting with laravel-excel for the first time. tangled. Many times I tried to install version 2.1 to make it easy to use, I received the advice of the guys “Because you do not understand” so I took a few hours to continue with what I needed in version 3.1.

II. Setting.

I do not need to write anything when the doc has written clearly how to install laravel, but for those who are lazy to not want to transfer the link, I copy the way to install on laravel-excel doc here for you to pase quickly from clicking it takes time to click through.

The following command is for the case where a new version is later available and you still want to use the 3.1 version

In config / app.php add providers

In config / app.php add aliases

To publish the config, run the vendor publish command. In the config, a new excel.php file is created for you to configure.

So that has finished installing it quite simply and gently.

III. Learn about the export data export feature.

The Maatwebsite / Laravel-Excel package provides us with a lot of features such as exporting files with multiple formats, exporting format, mapping data, exporting files via the view blade, … but in this article, I will only focus on export and what I find out. The other sections you can read more in the doc

1. Export a file.

First you need to create an app / Exports file

Next, type the following command in the terminal to create a file for the purpose of processing the data to be exported to the file and configuring the excel file when it is exported.

User is the model name you need to query for data, maybe yes, maybe not because when you add the file ExportFile.php is created it will use more that model. If not, it will not be used.

You can see that this line of user App User is added automatically because when creating the file, we have –model = User. Now quickly create the controller:

And the router:

What’s missing is that the user table can also export the file.

Open the newly created migrate file and add some data fields, don’t forget to create the User model.

Remember to add the data to the users table and access the router path and you will see the excel file has been downloaded.

2. Customize.

So above I have briefly demonstrated how to export excel file with laravel-excel. Quite easy is not it, actually on the doc also wrote and not too detailed, but anyone who knows about laravel can follow.

2.1 Transfer data from the controller to the export file.

Surely you understand what I mean. When doing the file export, I have quite a problem that the controller side needs to process the data first and then export it or when I want to export the user’s data but must follow the id. The problem is that on the export file side as in the example ExportFile.php needs the id on the router to get the user’s data to export to the excel file. I transferred the data from the controller via ExportFile.php as follows.

The controller side, before downloading the file, I will transfer the processed data to ExportFile.php and then download the data for example:

As above, you can optionally send data to the side of the excel export file to solve the problem as I gave above, here I can freely process the data from before and then transfer it to export. excel file. You can also pass other values ​​such as headings or whatever else you need. Ok now my specific export file is ExportFile.php (this file is created from the example above in app / Exports, lest you don’t remember).

Here you can see yourself using FromArray and creating construct to get the data the controller pushes through. Now you can transfer the processed data from the controller to export to an excel file.

2.2 Automatically align the height and width.

With long data, you will probably find that there is a problem when text is hidden or not displaying all the text. At this point, I need to automatically align all the rows to display all data without being obscured. Here I use ShouldAutoSize as simple as follows:

2.3 Create headings into columns.

This is also said on the doc but is created horizontally if you notice that it can be easily created vertically by returning multiple arrays as follows.

Remember that this WithHeadings user on the doc is quite clear, so my example is over.

2.4 Merge rows and align numbers to the left.

Of course, the merging of rows often happens in excel, and the number is usually to the right of the excel cell, here we also have a solution for these two problems.

Here you can easily see that you can also set a value for each cell of excel.

2.5 Adjust the size and color.

In section 2.4 you can see that I have a user that RegistersEventListeners do not use, because I deleted that now I can use it.

With the registerEvents function, I can optionally edit the font, size, and colo of the excel file.

IV. Conclude.

So I just shared a bit of my understanding after learning through laravel-excel. Because we have not been able to learn for a long time, there are still many problems. Thank you! Reference: https://docs.laravel-excel.com/3.1/exports/

Share the news now

Source : Viblo