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.