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

边栏推荐
- JQ verifies whether the input color is legal
- PHP appends the same elements to a two-dimensional array
- Explain AHP in human language (very detailed principle + simple tool implementation)
- Is it safe to open an account and buy stocks on the Internet?
- 一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
- Development with courtesy -- share the source code of the secondary development of the app system of the imitation shopping mall
- A commonly used statistical modeling method -- difference analysis
- Zhangxiaobai's road of penetration (IV) -- detailed explanation of XSS cross site script vulnerabilities
- Thinkphp3 count ` *'problem
- Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability
猜你喜欢

Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

刷入Magisk通用方法

How to use SPSS to do grey correlation analysis? Quick grasp of hand-to-hand Teaching

Linear regression of common mathematical modeling models for College Students
![[论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer

Explain factor analysis in simple terms, with case teaching (full)

Time series analysis - how to use unit root test (ADF) correctly?

Dark horse shopping mall ---3 Commodity management

What is principal component analysis? Dimension reduction of classical case analysis variables
随机推荐
sudo: ulimit: command not found
New and old cluster migration of Minio data
confluence7.4. X upgrade record
Digital currency exchange -- digital currency exchange system development source code sharing
PHP multidimensional array sorting
揭秘GaussDB(for Redis):全面对比Codis
Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
Arm immediate
Penetration tool environment -- use of cknife Chinese kitchen knife
PHP parsing QR code content
Thinkphp3 reinforcement i() function filter single quotation marks
[regression analysis] understand ridge regression with case teaching
ECSHOP whole site custom URL supports directory type
Disconnected: No supported authentication methods available (server sent: )
【OceanBase】OceanBase简介及其与MySQL的比较
什么是Flink?Flink能用来做什么?
What is the primordial universe
Wechat forbids sharing
Windows下MySQL的安装和删除
Web project development process