当前位置:网站首页>laravel-excel导出数据,对数据进行样式修改,数据处理
laravel-excel导出数据,对数据进行样式修改,数据处理
2022-07-16 03:18:00 【偷鱼的贪吃猫】
Maatwebsite 3.1版本excel导出,对数据,样式进行处理,再此记录一下
下载laravel-excel,下载的是最新版本的,需要注意框架版本是否符合,框架版本低可以下载2.1
下载
composer require maatwebsite/excel
发布配置,会添加一个config/excel.php文件
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
创建 App\Exports\UsersExport.php
<?php
namespace App\Exports;
use App\Models\User;//引入用户表models类
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 = '司机数据表';//导出文件名
protected $titles = [];//标题
public function __construct()
{
$this->fileName = $this->fileName.'_'.Str::random(6).'.xlsx';//拼接下载文件名称
$this->titles = ['id' => 'id','name'=>'司机名称', 'car_id' => '所属车队' ,'created_at'=>'创建时间','updated_at'=>'更新时间'];
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();
}
//设置宽度
public function columnWidths(): array
{
//设置行宽度
return [
'A' => 15,
'B' => 35,
'C' => 35,
'D' => 35,
'E' => 35,
];
}
//自定义添加公式
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$rows_line = $event->sheet->getHighestRow();//获取当前总共有多少条数据
$count = $rows_line+1;//获取最后一行
$event->sheet->setCellValue("A$count","=SUM(A:A)");// 设置A列 最后一行的值,值为A列的总和
}
];
}
public function map($row): array
{
//可在里面对数据进行修改
$car = Motorcade::where('id',$row['car_id'])->first();
$row['car_id'] = $car['name'];//修改 $row['car_id']的值
// TODO: Implement map() method.
return [
$row['id'],
$row['name'],
$row['car_id'],
$row['created_at'],
$row['updated_at'],
];
}
}
在控制器中引入
use App\Exports\UsersExport;
$grid->export(new UsersExport());
导出来的数据
边栏推荐
- Where is the motivation to be a test / development programmer? How to persist
- Hcip third day learning notes
- Add right click to create a markdown file
- Implementation of MCU stack backtracking debugging principle based on gd32 c10x
- Google Earth Engine APP(GEE)—加载一个可查询的Spector
- [swoole series 2.4] websocket service
- 报错解决——台式机与U盘数据传输报错
- Decompression error -- error: entry too big to split, read, or write (poor compression result in unexpectedly large
- PPP综合实验
- A survey on dialogue systems: recent advances and new frontiers
猜你喜欢

MySQL foundation - add, delete, check and modify (Foundation)

解决pycharm无法输入中文的方法:

Outil de transfert de port rinetd

Pytoch -- error reporting solution: runtimeerror: expected all tensors to be on the same device, but found at least two

内存管理页面属性

hcip第二天

Still reading logs on the command line? Use kibana, visual log analysis yyds~

(note) ideavim and IDE conflict resolution

hcip第六天笔记

PPP综合实验
随机推荐
hcip静态综合复习实验
【Golang | gRPC】gRPC-Client Streaming客户端流实战
金仓数据库 KingbaseES SQL 语言参考手册 (3.1.1.10. 文本搜索类型、3.1.1.11. UUID类型)
mysql中一些功能相似的函数详解
内存管理页面属性
hcip动态路由实验(RIP)
How to systematize the introduction to data science for free?
【HBZ分享】TCP协议讲解
Dismiss professional operation and maintenance personnel
金仓数据库 KingbaseES SQL 语言参考手册 (3.1.1.12. XML类型)
Decompression error -- error: entry too big to split, read, or write (poor compression result in unexpectedly large
Dongfang selects the traffic password behind the explosion
【Golang | gRPC】gRPC-Bidirectional Streaming双向流实战
【HBZ分享】UDP协议讲解
解压报错——error: Entry too big to split, read, or write (Poor compression resulted in unexpectedly large
[swoole series 2.4] websocket service
Golang empty interface
Hcip third day learning notes
Word——设置Tab键宽度
ReversingKr-wp(7)