当前位置:网站首页>Laravel excel export
Laravel excel export
2022-06-25 12:35:00 【qq_ twenty-five million sixty thousand seven hundred and sixty-】
1、 install
Using the command line use composer install maatwebsite/excel Execute the following code
composer require maatwebsite/excel
Package manifest generated successfully. Indicates successful installation
open config/app.php file , Add the following code
'providers'=> [
Maatwebsite\Excel\ExcelServiceProvider::class,
]'aliases' => [ 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ],
Release configuration , Please run vendor publish command :
php artisan vendor:publish
A new configuration file will be created automatically config/excel.php
3、 usage
Create export class first , Take exporting users as an example
php artisan make:export UserExport
A new file will be created automatically app/Exports/UserExport.php
The following is the export class code (UserExport.php)
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class UserExport implements FromCollection
{
private $row;
private $data;
public function __construct($row,$data)
{
$this->row = $row;
$this->data = $data;
}
public function collection()
{
$row = $this->row;
$data = $this->data;
// Set the header
foreach ($row[0] as $key => $value) {
$key_arr[] = $key;
}
// input data
foreach ($data as $key => &$value) {
$js = [];
for ($i=0; $i < count($key_arr); $i++) {
$js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
}
array_push($row, $js);
unset($val);
}
return collect($row);
}
}4、 Calling method export file (UserController .php)
<?php
namespace App\Http\Controllers\Admin;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;
use App\Models\User as userModel;
class UserController extends Controller
{
/**
* User list Export
* @param Request $request
*/
public function user_export(Request $request){
// Set the header
$row = [[
"id"=>'ID',
"nickname"=>' The user nickname ',
"gender_text"=>' Gender ',
"mobile"=>' cell-phone number ',
"addtime"=>' Creation time '
]];
// data
$list=[
0=>[
"id"=>'1',
"nickname"=>' Zhang San ',
"gender_text"=>' male ',
"mobile"=>'18812345678',
"addtime"=>'2019-11-21 '
],
2=>[
"id"=>'2',
"nickname"=>' Li Si ',
"gender_text"=>' Woman ',
"mobile"=>'18812349999',
"addtime"=>'2019-11-21 '
]
]; // Perform export
return Excel::download(new UserExport($row,$list), date('Y:m:d ') . ' User list .xls');
}
}Export completed

边栏推荐
- PHP appends the same elements to a two-dimensional array
- 20. MVVM command binding of WPF
- Micro engine generates QR code
- JQ verifies whether the input color is legal
- High imitation blue playing network disk file sharing to make money network disk PHP system source code
- [oceanbase] Introduction to oceanbase and its comparison with MySQL
- VIM common commands and shortcut keys
- The first techo day Tencent technology open day in 2022 will be held online on June 28
- 一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
- Online blind box system development function introduction and some source code sharing
猜你喜欢

【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?

The server reported an error 503 service unavailable:the system returned: (71) protocol error

How to use ARIMA model for prediction?
![[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?](/img/51/9aceaaeed1f6db7c60685d53477eba.png)
[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?

Penetration tool environment -- use of cknife Chinese kitchen knife

Explain AHP in human language (very detailed principle + simple tool implementation)

Service charge and time setting code sharing involved in crmeb withdrawal process

揭秘GaussDB(for Redis):全面对比Codis

MySQL common interview questions

Penetration tool environment - installing sqli labs in centos7 environment
随机推荐
JQ verifies whether the input color is legal
Tp6 exception handling try catch writing method
Linear regression of common mathematical modeling models for College Students
【OceanBase】OceanBase简介及其与MySQL的比较
20、wpf之MVVM命令绑定
实现领域驱动设计 - 使用ABP框架 - 系列文章汇总
Introduction to jiuhongtianxia system development function -- jiuhongtianxia app development source code sharing
PHP replaces the key of a two-dimensional array with a specified element value
Polling and long polling
The whole page turns gray
Development with courtesy -- share the source code of the secondary development of the app system of the imitation shopping mall
2022 meisai D topic ideas sharing + translation
New and old cluster migration of Minio data
ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
R language uses GLM function to build Poisson logarithmic linear regression model, processes three-dimensional contingency table data to build saturation model, and poisgof function of epidisplay pack
K8s, docker compose install MySQL 8.0.18
15、wpf之button样式小记
Huikan source code -- Huikan app system secondary development source code sharing
Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
19、wpf之事件转命令实现MVVM架构