如何在 Laravel 项目中处理 Excel 文件
1.Laravel Excel
Laravel Excel 是一款基于PHPExcel开发的Laravel框架专用的Excel/CSV 文件导入导出功能的扩展包,用起来的非常方便。
它的Github地址是:https://github.com/Maatwebsite/Laravel-Excel
当然了,你也可以使用PHPExcel,但是请注意,PHPExcel官方团队已经停止维护了,现在官方团队开发维护的是它的升级版PHPExcel扩展包,叫做:PhpSpreadsheet
我们今天主要介绍(因为我前天项目中用到了导出,最后选择了Laravel Exxcel☺):Laravel Excel
安装
1). 使用 Composer 安装该扩展包 php7.1以上用3.1.0版本:
composer require "maatwebsite/excel:~3.1.0"
2).
安装完成后,修改 config/app.php 在 providers 数组内追加如下内容
- 'providers' => [
- ...
- Maatwebsite\Excel\ExcelServiceProvider::class,
- ],
3).
同时在 aliases 数组内追加如下内容:
- 'aliases' => [
- ...
- 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
- ]
4).
接下来运行以下命令生成此扩展包的配置文件 config/excel.php:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
5.创建导出类
php artisan make:export ExcelExport
<?php namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ExcelExport implements FromCollection, WithHeadings
{
use Exportable; private $data;
private $headings; //数据注入
public function __construct($data, $headings)
{
$this->data = $data;
$this->headings = $headings;
} //实现FromCollection接口
public function collection()
{
return collect($this->data);
} //实现WithHeadings接口
public function headings(): array
{
return $this->headings;
}
}
6.控制器里
public function export(){
$data = [
[
'name' => 'cheng',
'email' => 'cheng222'
],
[
'name' => 'cheng',
'email' => 'cheng111'
],
]; $headings = [
'name',
'email'
];
return Excel::download(new ExcelExport($data, $headings), 'users.csv'); }
导入文件
1.创建导入类
php artisan make:import ExcelImport
这里向和我一样不明白的小白说下,关于导入导出excel 文档里主要设计到三种 : 模型 model , 集合 collection , 数组 Array
此外还有 implements ToArray 和 implements ToCollection 两个方法,
<?php namespace App\Imports; use App\User;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel; class UsersImport implements ToModel
{
/**
* @param array $row
*
* @return User|null
*/
public function model(array $row)
{
return new User([
'name' => $row[],
'email' => $row[],
'password' => Hash::make($row[]),
]);
}
}
<?php namespace App\Imports; use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToArray; class UsersImport implements ToArray
{
public function Array(Array $tables)
{
return $tables;
} }
控制器的两种用法
$array = Excel::toArray(new UsersImport, 'users.xlsx'); $collection = Excel::toCollection(new UsersImport, 'users.xlsx');
最新文章
- 【Python基础学习五】列表,元祖,字典
- Vitamio
- Docker 不能被外网正常访问
- Debian 7(Linux) 安装SSH使用SecureCRT连接配置
- change和onchange触发为什么不立马生效?
- 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)
- 安装UnityVS 2012步骤
- 从零开始PHP学习 - 第五天
- 转:Loadrunner——Simulate a new user on each iteration设置
- Android 仿映客直播间给主播发送礼物(实现连击效果)
- 多源最短路径---Floyd-Warshall算法
- 经典案例之MouseJack
- SQL SERVER之查询外键及索引
- NodeJs连接操作MongoDB数据库
- js实现小功能 动态赋值
- 正则表达式零宽断言详解(?=,?<;=,?!,?<;!)
- 怎么让Windows2012和Windows2008多用户同时远程---经过测试有效
- Async异步编程入门示例
- KrakenD: API Gateway and Manager
- NameNode的ZKFC机制