2017 © Pedro Peláez
 

yii2-extension yii2-excel-exporter

Helper to export Active Records to CSV

image

ruskid/yii2-excel-exporter

Helper to export Active Records to CSV

  • Tuesday, October 27, 2015
  • by ruskid
  • Repository
  • 1 Watchers
  • 0 Stars
  • 100 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 203 % Grown

The README.md

Yii2 CSV Exporter

This will export Active Records into Excel file, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist ruskid/yii2-excel-exporter "dev-master"

or add, (*4)

"ruskid/yii2-excel-exporter": "dev-master"

to the require section of your composer.json file., (*5)

Usage

 $exporter = new CSVExporter;
//Add headers A1-A3
$exporter->setCellValue('A1', 'Subcategoria');
$exporter->setCellValue('A2', 'Propietario');
$exporter->setCellValue('A3', 'Escenario');
//Apply header styles for A1-A3
for ($i = 1; $i <= 3; $i++) {
    $exporter->setCellStyleFromArray('A' . $i, $exporter->headerStyle);
}
//Set values to B1-B3
$exporter->setCellValue('B1', $model->name);
$exporter->setCellValue('B2', $model->propietario);
$exporter->setCellValue('B3', $model->escenario);

//Set server data. start from line A11. will return next free cell index
$nextCellIndex = $exporter->setCellData('A11', $model->servers, [
    [
        'header' => Yii::t('app', 'HOSTNAME'),
        'value' => function($array) {
            return $array['HOSTNAME'];
        }
    ],
    [
        'header' => Yii::t('app', 'DESC_CATALOGO'),
        'value' => function($array) {
            return $array['DESC_CATALOGO'];
        }
    ],
]);

//set users from next cell free cell index
$users = \app\models\User::find()->all();
$exporter->setCellData($nextCellIndex, $users, [
    [
        'header' => Yii::t('app', 'Login'),
        'value' => function($object) {
            return $object->username;
        }
    ],
]);

$exporter->filename = 'test';
return $exporter->export();
  • You can also add styling, see style and headerStyle parameters.

The Versions

27/10 2015

dev-master

9999999-dev

Helper to export Active Records to CSV

  Sources   Download

GPL-2.0

The Requires

 

by Victor Demin

yii2 exporter excel