如何使用Laravel将(Oracle)数据库表导出到Excel工作表

时间:2019-03-03 20:26:52

标签: excel laravel export

我正在尝试使用laravel将(查询结构化的)表(称为形式表)导出为ex​​cel。到目前为止,我可以使用此刀片在浏览器中将表显示为html。我想当用户按下“导出到excel”按钮导出到excel(我的刀片在下面):

<!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>

我没有成功遵循本指南。 https://docs.laravel-excel.com/3.1/getting-started/installation.html https://docs.laravel-excel.com/3.1/exports/

maatwebsite / excel已成功安装。但是没有excel下载。

1 个答案:

答案 0 :(得分:0)

问题已解决。我这样做是为了导出到excel文件。

到目前为止,以下是我的解决方案。谢谢大家!

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


        ];
    }
}