承接 cyber-duck/laravel-excel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cyber-duck/laravel-excel

Composer 安装命令:

composer require cyber-duck/laravel-excel

包简介

This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection.

README 文档

README

Latest Stable Version Total Downloads License

Exporting and importing Excel, CSV and OpenOffice stylesheets using Eloquent Collections and Query Builders in Laravel (5.* and 4.*).
It's based on box/spout.

Author: Simone Todaro
Contributors: Clément Blanco
Made with ❤️ by Cyber-Duck Ltd

Installation
Export Excel
Import Excel
Different formats

Installation

Use composer to download the package:

composer require cyber-duck/laravel-excel

Laravel 4.x

Register the service provider in config/app.php by adding this line to providers array.

'providers' => [
	Cyberduck\LaravelExcel\ExcelLegacyServiceProvider::class,
],

Laravel < 5.5

Register the service provider in config/app.php by adding this line to providers array.

'providers' => [
	Cyberduck\LaravelExcel\ExcelServiceProvider::class,
],

Laravel > 5.5

No need to register anything, since it used package auto discovery feature in Laravel 5.5.

Export Excel

Generate and download an excel file

Add

use Exporter;

to your controller.

In your controler function, create a new excel file from an Eloquent collection.

$excel = Exporter::make('Excel');
$excel->load($yourCollection);
return $excel->stream($yourFileName);

The exporter class is fluent, so you can also write

return Exporter::make('Excel')->load($yourCollection)->stream($yourFileName);

The exporter class supports Query builder objects as well

$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
return $excel->stream($yourFileName);

If you deal with big tables, you can set the chunk size to minimise the memory usage

$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
$excel->setChunk(1000);
return $excel->stream($yourFileName);

Generate and save an excel file

To save the excel file on the server, use the save method.

return $excel->save($yourFileNameWithPath);

Advanced usage

By default, every element of the Collection becomes a row and every unprotected field of the Model becomes a cell.
No headers row is printed.

To change this behaviour, create a class extending Cyberduck\LaravelExcel\Contract\SerialiserInterface, implement the methods getHeaderRow() and getData(Model $data) and set this class on the excel object usint setSerialiser().

$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$excel->setSerialiser($serialiser);
return $excel->stream($yourFileName);

getHeaderRow() must return an array of string where every element is a cell of the first row. To not print the header row, simply return a void array [].
getData(Model $data) must return an array of string, and every elements is a cell.

Example

namespace App\Serialisers;

use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;

class ExampleSerialiser implements SerialiserInterface
{
    public function getData($data)
    {
        $row = [];

        $row[] = $data->field1;
        $row[] = $data->relationship->field2;

        return $row;
    }

    public function getHeaderRow()
    {
        return [
            'Field 1',
            'Field 2 (from a relationship)'
        ];
    }
}

then set the serialiser before saving the file the collection.

$collection = Exporter::make('Excel')->load($yourCollection)->setSerialiser(new ExampleSerialiser)->stream($yourFileName);

Import Excel

Add

use Importer;

to your controller.

In your controler function, import an excel file.

$excel = Importer::make('Excel');
$excel->load($filepath);
$collection = $excel->getCollection();
//dd($collection)

The importer class is fluent, then you can also write

return Importer::make('Excel')->load($filepath)->getCollection();

Advanced usage

By default, every row of the first sheet of the excel file becomes an array and the final result is wraped in a Collection (Illuminate\Support\Collection).

To import a different sheet, use setSheet($sheet)

$excel = Importer::make('Excel');
$excel->load($filepath);
$excel->setSheet($sheetNumber);
$collection = $excel->getCollection();
//dd($collection)

To import each row in an Eloquent model, create a class extending Cyberduck\LaravelExcel\Contract\ParserInterface and implement the methods transform($row).

Example

namespace App\Parsers;

use App\Models\YourModel;
use Cyberduck\LaravelExcel\Contract\ParserInterface;

class ExampleParser implements ParserInterface
{
    public function transform($row, $header)
    {
        $model = new YourModel();
        $model->field1 = $row[0];
        $model->field2 = $row[1];
        // We can manunipulate the data before returning the object
        $model->field3 = new \Carbon($row[2]);
        return $model;
    }
}

then set the parser before creating the collection.

$collection = Importer::make('Excel')->load($filepath)->setParser(new ExampleParser)->getCollection();

Different formats

The package supports ODS and CSV files.

ODS

$exporter = Exporter::make('OpenOffice');
$importer = Importer::make('OpenOffice');

CSV

$exporter = Exporter::make('Csv');
$importer = Importer::make('Csv');

cyber-duck/laravel-excel 适用场景与选型建议

cyber-duck/laravel-excel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 229.03k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2016 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「import」 「export」 「laravel」 「exporter」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cyber-duck/laravel-excel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cyber-duck/laravel-excel 我们能提供哪些服务?
定制开发 / 二次开发

基于 cyber-duck/laravel-excel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 229.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 78
  • 点击次数: 24
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 74
  • Watchers: 5
  • Forks: 32
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-06