D
D
dake12312017-08-11 07:43:38
Laravel
dake1231, 2017-08-11 07:43:38

How to export to excel on laravel?

Hello! Unable to export excel file to Laravel. I use the maatwebsite / excel library, I do it as in the documentation through the export method, it tries to send the headers, but then an error occurs - the headers have already been sent. Enter action code in controller

public function excel()
    {
        $filename = 'excel_export'.time();
        Excel::create($filename, function($excel){
            $excel->sheet('first tab', function($sheet){
                $callsQuery = DB::table('calls');
                $calls = $callsQuery->get();
               $sheet->loadView('home.first', array('calls' => $calls));
            });
        })->export('xls');
    }

Result
159c104f42b6483c8f94a13aabc198ea.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vimes7, 2017-08-11
@dake1231

Check if there is a random space before the <?php tag and if the output functions are called in some middleware

A
ajaxtelamonid, 2017-08-11
@ajaxtelamonid

Google "maatwebsite/excel headers already sent", first result, "call string() instead of export() then wrap up result in your response"
If that doesn't work, google further.

W
Welaurs, 2017-08-12
@Welaurs

The issue seems to be still unresolved. I also advise you to check the file encoding - try between UTF-8 and UTF-8-BOM

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question