Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Export an existing view to Laravel-Excel

Hello,

I have read the Laravel-Excel documentation to see how it's possible to export datas to Excel from a view.

In the example the export class generates the datas.

But I'd like to do that another way :

  • retrieve the datas from the database

  • display these datas in a view

  • click on a button to export the view to Excel by retrieving the data directly from the view

That's probably not the best idea because the datas (in the view) are on the front whereas I need to generate the Excel file on the back.

Why do I want to do this way ? Because the query is heavy and I don't want to call once again the database.

Is there any possibility ?

Thanks a lot.

V

0 likes
6 replies
LaryAI's avatar
Level 58

Yes, it is possible to export an existing view to Laravel-Excel. You can use the view() method to render the view and then pass the rendered HTML to the download() method of Laravel-Excel.

Here's an example:

use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;

// Retrieve the data from the database
$data = DB::table('my_table')->get();

// Render the view with the data
$html = View::make('my_view', compact('data'))->render();

// Export the view to Excel
return Excel::download(function($excel) use ($html) {
    $excel->sheet('Sheet 1', function($sheet) use ($html) {
        $sheet->fromHTML($html);
    });
}, 'my_file.xlsx');

In this example, we retrieve the data from the database, render the view with the data, and then pass the rendered HTML to the download() method of Laravel-Excel. The download() method takes a closure that receives an instance of the Excel class. We use this instance to create a new sheet and populate it with the HTML content using the fromHTML() method.

Note that this approach has some drawbacks, as mentioned in the question. The data is being retrieved from the view, which is not ideal. It's better to retrieve the data from the database and generate the Excel file on the back-end. However, if the query is heavy and you don't want to call the database again, this approach can be a viable solution.

1 like
vincent15000's avatar

@LaryAI That's not a bad idea, but in this case I necessarily will download the Excel file, but I want to download it only if I click on a button.

Furthermore this solution renders the view, but the view doesn"t display on the screen.

I want the data to be displayed on the screen.

I thought about a solution that would be to save the datas in a table, this could avoid to do twice the calculations (once to display the view and once to generate the Excel file).

Is there a better idea ?

Snapey's avatar
Snapey
Best Answer
Level 122

You could cache the query?

2 likes
kokoshneta's avatar

That's probably not the best idea because the datas (in the view) are on the front whereas I need to generate the Excel file on the back.

Why do I want to do this way ? Because the query is heavy and I don't want to call once again the database.

These are incompatible statements. Once the view is rendered in the user’s browser, the data fetched from the database is gone from the backend server – and so is the view. It doesn’t matter whether you generate the Excel file from a new database query in a class method or from view data, because neither is available on the server when the user clicks the button, so a new database query will be necessary in either case.

As @snapey says, you could cache the query to keep the data available on the server – but of course, if the data in the database changes and you just fetch from cache, you won’t get the most up-to-date data in your Excel file. That’s probably not an issue for User 1 who loads the view (generating the cache) and then clicks on the button to download the Excel file, but if User 2 comes along three hours later and does the same thing, and your app loads the data from cache, it may be an issue. So you’d have to cache the data when loading the view, and then clear the cache after you’ve generated the Excel file.

Alternatively, you could make the view a full-page Livewire component and store the generated data in the component. You’d then use the same data to load the view and generate the Excel file. I think that’s how I would do it – easier than caching and uncaching on every page load.

1 like
vincent15000's avatar

@snapey @kokoshneta I don't know who receives the best answer reward ;), both approches are very good ideas and very helpful ... let me a while to think about and test both solutions.

Please or to participate in this conversation.