Laravel Excel Export: Formatting and Styling Cells

Laravel-Excel package is great for exporting data. But not a lot of info there about formatting Excel cells – widths, word wraps, fonts etc. So when I encountered this in a client’s project, I decided to write this article with a few tips on this topic.

Default Laravel Export

Let’s create a dummy project with Users table and default Laravel Auth. Then we create 10 random users. Actually, Laravel comes with pre-built Factory class for User model, so all we need to do is to create a UsersTableSeeder.php

Then, we require Laravel Excel package:

Now, we export all users to Excel. To do that, we create a separate class app/Exports/UsersExport.php:

Finally, let’s use it from Controller:

Here’s the visual result:

It does the job, exports the data. But does it look good and readable? Far from it. So let’s tweak it a little.

Step 1. Auto-formatting column widths

Somewhere deep in the official documentation I’ve found a really easy way to adjust the column sizes automatically. All you need is to use one Trait ShouldAutoSize:

And here’s the new Excel:

Better, huh? Let’s move on.

Step 2. Formatting the fonts and sizes

You can adjust any cell styles using method called registerEvents() and AfterSheet event:

Here’s how the header row looks now:

The main logic here is this line:

It just takes whatever cell range we pass, and changes the styles.
Here’s where we need to dig deeper and look at the package which is the base of Laravel Excel. It’s actually based on PHPSpreadsheet package. So let’s take a look at its documentation, and specifically section Recipes.

Some examples from there:

Wrap text:

Default style of a worksheet:

Styling borders and applying styles from array:

You can find more examples in that recipes section, just apply it to your individual needs.

That’s it, that’s how you format exported excel with Laravel and PHP.

Related articles:

Want to generate Laravel adminpanel online?
You don't need any packages to do that!

5 thoughts on “Laravel Excel Export: Formatting and Styling Cells

  1. Hello.

    I was wondering, what about the route? and if I want to click a button in a blade view to trigger the download, how should I do it? Thanks!

    1. Hi, sorry, I didn’t mention this in the article, cause I thought it was pretty basic thing people would figure out.

      In this article I’m using controller ExportController and method export(), so you need to add this line to your routes:
      Route::get('export', 'ExportController@export')->name('export');

      And then somewhere in your blade files:
      <a href="{{ route('export') }}">Export</a>

  2. Hi,
    I was wondering in which file do I put this line :

  3. Beautiful article, but in the case of importing a spreadsheet file using this package how do we catch errors, let’s say an invalid file was uploaded, also in the case where we expect the uploaded file to have some named headings how do we check if the headings are there? Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *