当前位置:网站首页>Laravel excel exports data, modifies the style of data, and processes data
Laravel excel exports data, modifies the style of data, and processes data
2022-07-18 13:21:00 【Greedy cat stealing fish】
Maatwebsite 3.1 edition excel export , Data pair , Style to handle , Record again
download laravel-excel, Download the latest version , Pay attention to whether the Framework version conforms to , The Framework version is low and can be downloaded 2.1
download
composer require maatwebsite/excel
Release configuration , It will add a config/excel.php file
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
establish App\Exports\UsersExport.php
<?php
namespace App\Exports;
use App\Models\User;// Import user table models class
use App\Models\Motorcade;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithPreCalculateFormulas;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class UsersExport extends AbstractExporter implements WithMapping, WithHeadings, FromCollection,WithColumnWidths,
WithPreCalculateFormulas,WithEvents
{
use Exportable;
protected $fileName = ' Driver data sheet ';// Export filename
protected $titles = [];// title
public function __construct()
{
$this->fileName = $this->fileName.'_'.Str::random(6).'.xlsx';// Splice download file name
$this->titles = ['id' => 'id','name'=>' Driver name ', 'car_id' => ' Team ' ,'created_at'=>' Creation time ','updated_at'=>' Update time '];
parent::__construct();
}
public function export()
{
// TODO: Implement export() method.
$this->download($this->fileName)->prepare(request())->send();
exit;
}
public function collection()
{
// TODO: Implement collection() method.
return collect($this->buildData());
}
public function headings(): array
{
// TODO: Implement headings() method.
return $this->titles();
}
// Set width
public function columnWidths(): array
{
// Set row width
return [
'A' => 15,
'B' => 35,
'C' => 35,
'D' => 35,
'E' => 35,
];
}
// Custom add formula
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$rows_line = $event->sheet->getHighestRow();// Get the current total number of data
$count = $rows_line+1;// Get the last line
$event->sheet->setCellValue("A$count","=SUM(A:A)");// Set up A Column The value of the last line , The value is A The sum of the columns
}
];
}
public function map($row): array
{
// Data can be modified inside
$car = Motorcade::where('id',$row['car_id'])->first();
$row['car_id'] = $car['name'];// modify $row['car_id'] Value
// TODO: Implement map() method.
return [
$row['id'],
$row['name'],
$row['car_id'],
$row['created_at'],
$row['updated_at'],
];
}
}
Introduce
use App\Exports\UsersExport;
$grid->export(new UsersExport());
Exported data 
边栏推荐
- Kingbasees SQL language reference manual of Jincang database (3.1.1.8. geometric type)
- [machine learning] decision tree
- 剑指 Offer 26. 树的子结构
- 160_ Skills_ Power Bi new function - calculate working days
- mysql触发器和存储过程
- 剑指 Offer 32 - II. 从上到下打印二叉树 II for_in range()
- [machine learning] automatic encoder - autoencoder
- [D1 dock Pro development board] Quick Start Manual
- 剑指 Offer 48. 最长不含重复字符的子字符串
- Briefly describe the working principle of memcached
猜你喜欢
随机推荐
EXCEL,选择数据如何选择合适的图表?
The share price fell through! Is Muse going to pay for the "IQ tax"?
What capabilities should an excellent smart exhibition hall have
【RT-Thread】nxp rt10xx SFUD和FAL组件搭建与使用
Today's job insights
STL tips
Community summit pulsar summit old golden peak conference topic highlights exposure!
Summary of various parameters of ultra micro motherboard
缩减50%调试成本 小匠物联推可远程的串口调试助手
[machine learning] decision tree
示波器的带宽介绍
吉时利万用表的低功耗测试方案
Aomei Easy clone system disk backup
Sword finger offer 47 Maximum value of gifts
Mysql5.7创建用户错误:ERROR 1364 (HY000): Field ‘ssl_cipher‘ doesn‘t have a default value解决方法
社区峰会|Pulsar Summit 旧金山峰会议题亮点曝光!
ReversingKr-wp(4)
gan网络学习笔记
Task-Customized Self-Supervised Pre-training with Scalable Dynamic Routing
Efcore - entry and attach


![[notes] cryptography from introduction to earth | AES](/img/f8/6649d68018dad3c57b0864b4be0edd.png)

![[machine learning] random forest](/img/d7/27fa4a0d88a5d16641af6221b94f0e.png)


