GithubHelp home page GithubHelp logo

carlin-rj / laravel-excel Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 2.0 75 KB

Laravel-excel基于SpartnerNL/Laravel-Excel代码上,切换成xlswriter扩展。 如果您的项目使用的是SpartnerNL/Laravel-Excel并且出现大数据导出性能问题,你不想修改大量的代码,那么当前的包可能会很适合你

License: Apache License 2.0

PHP 99.73% Shell 0.27%
excel laravel laravel-excel php8 xlswriter

laravel-excel's Introduction

Laravel-excel 一款基于xlswriter的laravel扩展包

Latest Stable Version Total Downloads Latest Stable Version PHP Version Require License

xlswriter是一款高性能的php excel读写扩展,Laravel-excel基于SpartnerNL/Laravel-Excel代码上,切换成xlswriter扩展。 如果您的项目使用的是SpartnerNL/Laravel-Excel并且出现大数据导出性能问题,你不想修改大量的代码,那么当前的包可能会很适合你。 当然目前的包不可能百分之百兼容所有功能,目前只实现了部分基础的功能。

Xlswriter文档

如果本扩展帮助到了你 欢迎star。

如果本扩展有任何问题或有其他想法 欢迎提 issue与pull request。

Laravel-excel使用教程

环境要求

  • xlswriter >= 1.3.7
  • PHP >= 8.0 安装请按照XlsWriter的官方文档:安装教程

安装

composer require mckue/laravel-excel

发布mckue-excel.php配置文件:

php artisan vendor:publish --provider="Mckue\Excel\ExcelServiceProvider" --tag=config

1.命令

1.1 查看xlswriter扩展是否正常安装
 php artisan php-ext-xlswriter:status

展示信息如下:

info:
+---------+---------------------------------------------+
| version | 1.0                                         |
| author  | mckue<https://github.com/carlin-rj>             |
| docs    | https://github.com/carlin-rj/laravel-excel      |
+---------+---------------------------------------------+
XlsWriter extension status:
+-------------------------------+----------------------------+
| loaded                        | yes                        |
| xlsWriter author              | Jiexing.Wang ([email protected]) |
| xlswriter support             | enabled                    |
| Version                       | 1.3.7                      |
| bundled libxlsxwriter version | 1.0.0                      |
| bundled libxlsxio version     | 0.2.27                     |
+-------------------------------+----------------------------+

如您的信息展示如上所示,证明您的cli环境下本扩展可用。

1.快速开始

<?php

namespace App\Exports;

use Mckue\Excel\Concerns\FromArray;

class UserExport implements FromArray
{
 /** 
  * @return array */ 
  public function array() : array 
  { 
    return [ ['哈哈', 'aaa'],
         ['哈哈', 'aaa'],
         ['哈哈', 'aaa'],
         ['哈哈', 'aaa']
         ]; 
  }
 /** 
   * @return array 
   */ 
 public function headers() : array {
     return []; 
 }
}

🔥 在您的控制器中,您现在可以调用此导出:

<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use Mckue\Excel\Facades\Excel;

class UsersController extends Controller 
{
    public function export() 
    {
        return Excel::download(new UserExport, 'users.xlsx');
    }
}

最后添加一条能够访问导出的路由:

Route::get('users/export/', [UsersController::class, 'export']);

2.导出集合

InvoicesExport创建一个名为的新类app/Exports:

namespace App\Exports;

use App\Invoice;
use Mckue\Excel\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return Invoice::all();
    }
}

在您的控制器中,我们现在可以下载此导出:

public function export() 
{
    return Excel::download(new InvoicesExport, 'invoices.xlsx');
}

您可以选择传入是否输出标头和自定义响应标头:

public function export() 
{
    return Excel::download(new InvoicesExport, 'invoices.xlsx', true, ['X-Vapor-Base64-Encode' => 'True']);
}

或者将其存储在磁盘上(例如 s3):

public function storeExcel() 
{
    return Excel::store(new InvoicesExport, 'invoices.xlsx', 's3');
}

3.使用自定义结构

namespace App\Exports;

use App\Invoice;
use Mckue\Excel\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return new Collection([
            [1, 2, 3],
            [4, 5, 6]
        ]);
    }
}

4.使用查询

namespace App\Exports;

use App\Invoice;
use Mckue\Excel\Concerns\FromQuery;
use Mckue\Excel\Concerns\Exportable;

class InvoicesExport implements FromQuery
{
    use Exportable;

    public function query()
    {
        return Invoice::query();
    }
}

5.使用迭代器

namespace App\Exports;

use App\Invoice;
use Mckue\Excel\Concerns\FromIterator;
use Mckue\Excel\Concerns\Exportable;

class InvoicesExport implements FromIterator
{
    use Exportable;

    public function iterator(): Iterator
    {
        ...
    }
}

在前面的示例中,我们使用Excel::download Facades来启动导出。

namespace App\Exports;

use App\Invoice;
use Mckue\Excel\Concerns\FromCollection;
use Mckue\Excel\Concerns\Exportable;

class InvoicesExport implements FromCollection
{
    use Exportable;

    public function collection()
    {
        return Invoice::all();
    }
}

我们现在可以下载导出而无需Facades:

return (new InvoicesExport)->download('invoices.xlsx');

或者将其存储在磁盘上:

return (new InvoicesExport)->store('invoices.xlsx', 's3');

更多文档可参考WIKI

在此感谢 xlswriter的开发者viest 以及 SpartnerNL/Laravel-Excel的开发者。 如有什么问题可以及时反馈到github哦。

laravel-excel's People

Contributors

carlin-rj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

dmskys chick84

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.