Yii2 Excel Export

Yii2 Excel Export [ 2.0 版本 ]

Features
  • Export data from ActiveQuery results
  • Export any other data (Array, Iterable, ...)
  • Create excel files with multiple sheets
  • Format cells and values

Note: To write the Excel file, we use the excellent PHPExcel package.

Here's a quick example to get you started:

<?php
$file = \Yii::createObject([
    'class' => 'codemix\excelexport\ExcelFile',
    'sheets' => [
        'Users' => [
            'class' => 'codemix\excelexport\ActiveExcelSheet',
            'query' => User::find(),
        ]
    ]
]);
$file->send('user.xlsx');

Installation

Install the package with composer:

composer require codemix/yii2-excelexport *

Examples

For now we only provide some usage examples instead of a full documentation.

ActiveQuery results

<?php
$file = \Yii::createObject([
    'class' => 'codemix\excelexport\ExcelFile',

    // Default writer is `\PHPExcel_Writer_Excel2007`. You can use any other writer available from PHPOffice.
    //'writer' => '\PHPExcel_Writer_Excel5',

    'sheets' => [

        'Active Users' => [
            'class' => 'codemix\excelexport\ActiveExcelSheet',
            'query' => User::find()->where(['active' => true]),

            // If not specified, all attributes from `User::attributes()` are used
            'attributes' => [
                'id',
                'name',
                'email',
                'team.name',    // Related attribute
                'created_at',
            ],

            // If not specified, the label from the respective record is used.
            // You can also override single titles, like here for the above `team.name`
            'titles' => [
                'D' => 'Team Name',
            ],

            // 'formats' and 'formatters' are autogenerated for `ActiveExcelSheet`.
            // For example if `created_at` is a `DATETIME` column in the DB, the
            // column will be formatted as a date. You could still override formats
            // here though, for example if you save a timestamp as INT. See the
            // raw data example below.
        ],

    ],
]);
$file->send('demo.xlsx');

Raw data

<?php
$file = \Yii::createObject([
    'class' => 'codemix\excelexport\ExcelFile',
    'sheets' => [

        // Array keys are used as Sheet names in the final excel file
        'Result per Country' => [
            'data' => [
                ['fr', 'France', 1.234, '2014-02-03 12:13:14'],
                ['de', 'Germany', 2.345, '2014-02-05 19:18:39'],
                ['uk', 'United Kingdom', 3.456, '2014-03-03 16:09:04'],
            ],

            // Set to `false` to suppress the title row
            'titles' => [
                'Code',
                'Name',
                'Volume',
                'Created At',
            ],

            // Excel format strings
            'formats' => [
                'C' => '#,##0.00',
                // Instead of column names, the 0-based column index can be also used
                3 => 'dd/mm/yyyy hh:mm:ss',
            ],

            // You'll usually only need this for date columns
            'formatters' => [
                // Dates and datetimes must be converted to Excel format
                3 => function ($value, $row, $data) {
                    return \PHPExcel_Shared_Date::PHPToExcel(strtotime($value));
                },
            ],
        ],

        'Countries' => [
            // Data for another sheet goes here ...
        ],
    ]
]);
// Save on disk
$file->saveAs('/tmp/export.xlsx');

Query builder results

<?php
$file = \Yii::createObject([
    'class' => 'codemix\excelexport\ExcelFile',
    'sheets' => [

        'Users' => [
            'data' => new (\yii\db\Query)
                ->select(['id','name','email'])
                ->from('user')
                ->each(100);
            'titles' => ['ID', 'Name', 'Email'],
        ],
    ]
]);
$file->send('demo.xlsx');

转载于:https://my.oschina.net/u/3683692/blog/2983967

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值