Using LaravelExcel to implement file export function in Laravel5

不言
Release: 2023-03-25 15:50:01
Original
4904 people have browsed it

This article mainly introduces the file export function using LaravelExcel in Laravel5. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Using Laravel/Excel in Laravel5 Excel/CSV file export function

1. Installation
Here I installed version 2.1.0 of maatwebsite/excel

composer require "maatwebsite/excel:~2.1.0"
Copy after login

Settings after installation
In config/app Register the service provider in .php to the providers array:

Maatwebsite\Excel\ExcelServiceProvider::class,
Copy after login

Also register the facade to the aliases array in config/app.php:

'Excel' => Maatwebsite\Excel\Facades\Excel::class,
Copy after login

If you want to customize Laravel Excel more Define the configuration and execute the following Artisan command:

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
Copy after login

2. Export the data
Add routing

// excel exportRoute::get('/monitor/export','Admin\ExcelController@export')->name('monitor.export');
Copy after login

Create the controller

php artisan make:controller Admin/ExcelController
Copy after login

Add some code in the control

<?php
namespace App\Http\Controllers\Admin;use Illuminate\Http\Request;
use App\Http\Controllers\Controller;use Excel;use App\Models\Monitor;
use App\Exports\CunliangExport;class ExcelController extends Controller
{
    public function export()
    {
        //return Excel::download(new CunliangExport, &#39;invoices.xlsx&#39;);
        $data = Monitor::get()->toArray();        
        return Excel::create(&#39;数据更新&#39;, function($excel) use ($data) {
            $excel->sheet(&#39;数据更新&#39;, function($sheet) use ($data)
            {
                $sheet->cell(&#39;A1&#39;, function($cell) {$cell->setValue(&#39;update_date&#39;);   });                
                $sheet->cell(&#39;B1&#39;, function($cell) {$cell->setValue(&#39;file_type&#39;);   });                
                $sheet->cell(&#39;C1&#39;, function($cell) {$cell->setValue(&#39;file_num&#39;);   });                
                $sheet->cell(&#39;D1&#39;, function($cell) {$cell->setValue(&#39;space_size&#39;);   });                
                $sheet->cell(&#39;E1&#39;, function($cell) {$cell->setValue(&#39;exec_time&#39;);   });                
                $sheet->cell(&#39;F1&#39;, function($cell) {$cell->setValue(&#39;created_at&#39;);   });                
                if (!empty($data)) {                    
                foreach ($data as $key => $value) {                        
                $i= $key+2;                        
                $sheet->cell(&#39;A&#39;.$i, $value[&#39;update_date&#39;]);                        
                $sheet->cell(&#39;B&#39;.$i, $value[&#39;file_type&#39;]);                        
                $sheet->cell(&#39;C&#39;.$i, $value[&#39;file_num&#39;]);                        
                $sheet->cell(&#39;D&#39;.$i, $value[&#39;space_size&#39;]);                        
                $sheet->cell(&#39;E&#39;.$i, $value[&#39;exec_time&#39;]);                        
                $sheet->cell(&#39;F&#39;.$i, $value[&#39;created_at&#39;]);
                    }
                }
            });
        })->download(&#39;xlsx&#39;);
    }
}
Copy after login

Add the following code to the blade template file

.
.
.<p class="box-header">
    <a class="btn btn-success" href="{{route(&#39;monitor.export&#39;)}}">导出</a></p>.
.
.
Copy after login

Just change it according to your needs.

Related recommendations:

Laravel5 implements fuzzy matching function

Laravel5.5 new feature error reporting

The above is the detailed content of Using LaravelExcel to implement file export function in Laravel5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!