如何在 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版本:

 

 

[php]  view plain  copy
  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[0],
           'email'    => $row[1], 
           'password' => Hash::make($row[2]),
        ]);
    }
}

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM