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

如何在 Laravel 项目中处理 Excel 文件的更多相关文章

  1. [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger

    如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...

  2. C#项目中操作Excel文件——使用NPOI库

    转载自:http://blog.csdn.net/dcrmg/article/details/52356236# 感谢-牧野- 实际C#项目中经常会涉及到需要对本地Excel文件进行操作,特别是一些包 ...

  3. Vue项目中导入excel文件读取成js数组

    1. 安装组件 cnpm install xlsx --save 2. 代码 <template> <span> <input class="input-fil ...

  4. vue项目中导出Excel文件功能的前端代码实现

    在项目中遇到了两种不同情况, 1.get请求导出文件,实现起来相对简单 // 导出数据 exportData() { window.location.href = `/oes-content-mana ...

  5. #iPhone6与iPhone6Plus适配#如何在Xcode 6中创建 PCH 文件

    本文永久链接http://www.cnblogs.com/ChenYilong/p/4008086.html   新建文件 ⌘+N选择 iOS/Mac -> Other -> PCH Fi ...

  6. 如何在cocos2d项目中enable ARC

    如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...

  7. 如何在NodeJS项目中优雅的使用ES6

    如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...

  8. 如何在VUE项目中添加ESLint

    如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...

  9. 如何在mvc项目中使用apiController

    文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...

随机推荐

  1. Java模版引擎之Freemarker

    Java模版引擎之Freemarker freemarker是一款模版引擎,是一种基于模版生成静态文件的通用工具,它是为Java程序员提供的一个类库,它不是面向最终用户的,而是为程序员提供了一款可以嵌 ...

  2. 运算符优先级 以及 && (逻辑与) 和||(逻辑或)的优先级:

    运算符优先级(从高到低列出) 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型. ...

  3. Dom4j 生成xml并格式化

    Document document = DocumentHelper.createDocument(); //创建root         Element root = document.addEle ...

  4. 数据结构:BF算法

    贴上源代码: #include<iostream> using namespace std; int BF(char S[],char T[]) { int i,j; i = j = 0; ...

  5. python+selenium+chrome实现自动登录百度

    #python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...

  6. java对象序列化并存储到文件中

    ● 如何将一个Java对象序列化到文件里 使用输入输出流,,一个是ObjectOutputStream 对象,ObjectOutputStream 负责向指定的流中写入序列化的对象.当从文件中读取序列 ...

  7. php curl post请求

    /** * CreateBy Song * @param String $url url地址 * @param Array $post url参数 * @return Array */ functio ...

  8. .js文件中文乱码解决办法

    描述:.js文件里的中文内容在网页中显示乱码 解决办法:把JS文件的编码改为utf-8 VS2013解决步骤:文件——高级保存选项——Unicode (UTF-8带签名)  代码页 65001

  9. chartjs显示数值标签插件:chartjs-plugin-datalabels

    Getting Started #Installation #npm   npm install chartjs-plugin-datalabels --save This plugin can al ...

  10. JS数组判断,方法

    怎么判断一个对象是不是数组? 首先可以用 ES5 提供的 isArray 方法进行判断(注意:Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题. ) ...