1

I am trying to export a (query structured) table (called formal) to excel using laravel. So far I can show table in browser as html using this blade. I want when user presses Export to excel button to export to excel (my blade is below):

<!DOCTYPE html>
<html lang="el">
<head>
    <meta charset="UTF-8">
    <title>Studies</title>
</head>
<body>
<b>Schedule: {{ $key }}</b>
<br>
<table border="1">
    <tr>
        <th>Lesson Category</th>
        <th>Lesson Code</th>
        <th>Lesson Title</th>
        <th>Lesson Department</th>
          /* extra columns
           ....
          */
        <th>Audience</th>
    </tr>
    @foreach($star as $v2)
    <tr>
        <th>{{ $v2->lesson_category }}</th>
        <td>{{ $v2->lesson_code }}</td>
        <td>{{ $v2->lesson_title }}</td>
        <td>{{ $v2->lesson_dep }}</td>
        /* extra columns
           ....
        */
        <td>{{ $v2->audience}}</td>
    </tr>
    @endforeach
</table>
<br>

<form action='/programma'>
    <input type="submit" value="New Search" />
</form>

<br>

<form action='/export'>
    <input type="submit" value="Export to excel" />   //I want this button to 
                                                      //export to excel
</form>

</body>
</html>

I have followed this guide with no success. https://docs.laravel-excel.com/3.1/getting-started/installation.html https://docs.laravel-excel.com/3.1/exports/

The maatwebsite/excel has been installed successfully. But no excel is downloaded.

3
  • Please can you show the controller code for the /export route? Commented Mar 3, 2019 at 21:44
  • What error do you get after calling '/export' ? Do you have all required extensions enabled server-side? Commented Mar 4, 2019 at 0:13
  • Hi guys! I finally did it, but there is still a problem. The exported excel does not have titles on top of each columns. Could you help me a little bit? I will answer my question myself and any idea is very very welcome! Commented Mar 4, 2019 at 8:06

1 Answer 1

0

The problem is solved. I did it to export to excel file.

Below is my solution so far. Thank you all!

1) web.php

Route::get('/exagogi','UsersController@export');

2) UsersController.php

<?php namespace App\Http\Controllers;

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UsersController extends Controller 
{
    public function export() 
    {
        return Excel::download(new UsersExport, 'result.xlsx');
    }
}

3) UsersExport.php

<?php

namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings; 

class UsersExport implements FromCollection, WithHeadings
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }

    public function headings(): array
    {
        return [
            'Lesson Category',
            'Lesson Title',
            'Lesson Department'
        // etc


        ];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.