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版本:

  1. composer require "maatwebsite/excel:~3.1.0"

2).
安装完成后,修改 config/app.php 在 providers 数组内追加如下内容

[html] view plain copy
  1. 'providers' => [
  2. ...
  3. Maatwebsite\Excel\ExcelServiceProvider::class,
  4. ],

3).
同时在 aliases 数组内追加如下内容:

[html] view plain copy
  1. 'aliases' => [
  2. ...
  3. 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
  4. ]

4).
接下来运行以下命令生成此扩展包的配置文件 config/excel.php:

[html] view plain copy

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');

最新文章

  1. 【Python基础学习五】列表,元祖,字典
  2. Vitamio
  3. Docker 不能被外网正常访问
  4. Debian 7(Linux) 安装SSH使用SecureCRT连接配置
  5. change和onchange触发为什么不立马生效?
  6. 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)
  7. 安装UnityVS 2012步骤
  8. 从零开始PHP学习 - 第五天
  9. 转:Loadrunner——Simulate a new user on each iteration设置
  10. Android 仿映客直播间给主播发送礼物(实现连击效果)
  11. 多源最短路径---Floyd-Warshall算法
  12. 经典案例之MouseJack
  13. SQL SERVER之查询外键及索引
  14. NodeJs连接操作MongoDB数据库
  15. js实现小功能 动态赋值
  16. 正则表达式零宽断言详解(?=,?&lt;=,?!,?&lt;!)
  17. 怎么让Windows2012和Windows2008多用户同时远程---经过测试有效
  18. Async异步编程入门示例
  19. KrakenD: API Gateway and Manager
  20. NameNode的ZKFC机制

热门文章

  1. ValueError: row index was 65536, not allowed by .xls format
  2. Web安全小结之前端
  3. pb菜单详解和MDI
  4. [Lua]LuaAPI整理
  5. docker 入门5 - 栈 【翻译】
  6. where用法
  7. 使ffmpeg支持HDR10bit 环境为ubuntu16.04
  8. SIP协议入门:初学者必须明白的几个重要概念
  9. Javascript的学习清单
  10. Short XSS