当前位置:网站首页>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

边栏推荐
- ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities
- Implementing Domain Driven Design - using the ABP framework - Summary of a series of articles
- Rank sum ratio comprehensive evaluation method for common models in mathematical modeling
- R language uses the scale function to scale the input data of neural network to the minimum and maximum, scale the data to between 0 and 1, and divide the data set into training set and test set
- The R language cartools package divides data, the scale function scales data, and the NaiveBayes function of e1071 package constructs a naive Bayesian model
- The dist function of R language calculates the distance between two samples in dataframe data, returns the distance matrix between samples, and specifies the distance calculation method through the me
- Zhangxiaobai's road of penetration (IV) -- detailed explanation of XSS cross site script vulnerabilities
- K8s, docker compose install MySQL 8.0.18
- R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter, spe
- The first techo day Tencent technology open day in 2022 will be held online on June 28
猜你喜欢

MySQL common interview questions

Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting

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

20. MVVM command binding of WPF

What is principal component analysis? Dimension reduction of classical case analysis variables

Penetration tool environment -- use of cknife Chinese kitchen knife

19、wpf之事件转命令实现MVVM架构

Why do we do autocorrelation analysis? Explain application scenarios and specific operations

ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function

什么是Flink?Flink能用来做什么?
随机推荐
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information
20. MVVM command binding of WPF
R language dplyr package filter function filters the data rows in the specified list whose contents in the dataframe data are not (not equal to one of the specified vectors)
Arm V7 LDR STR memory access
ECSHOP product attribute color specification size stock item No. automatic combination
Record a debugging when curl has no return value
[regression analysis] understand ridge regression with case teaching
Set the transparency of the picture to gradient from left to right
MySQL common interview questions
sudo: ulimit: command not found
Array reorder based on a field
R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter, spe
Error while sending STMT_ PREPARE packet. PID=29294
Digital currency exchange -- digital currency exchange system development source code sharing
New and old cluster migration of Minio data
devsecops与devops的理解与建设
A commonly used statistical modeling method -- difference analysis
Spicy food advertising e-commerce system development function and spicy food advertising e-commerce app system development source code sharing
Understanding and construction of devsecops and Devops
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing