Class humhub\components\export\SpreadsheetExport

Inheritancehumhub\components\export\SpreadsheetExport » yii\base\Component

SpreadsheetExport allows export of data into PhpSpreadsheet It supports exporting of the \yii\data\DataProviderInterface and \yii\db\QueryInterface instances.

This class was originally developed by Paul Klimov klimov.paul@gmail.com and his project csv-grid (https://github.com/yii2tech/csv-grid).

Example:

use humhub\components\export\SpreadsheetExport;
use yii\data\ArrayDataProvider;

$exporter = new SpreadsheetExport([
    'dataProvider' => new ArrayDataProvider([
        'allModels' => [
            [
                'name' => 'some name',
                'price' => '9879',
            ],
            [
                'name' => 'name 2',
                'price' => '79',
            ],
        ],
    ]),
    'columns' => [
        [
            'attribute' => 'name',
        ],
        [
            'attribute' => 'price',
            'format' => 'decimal',
        ],
    ],
]);
$exporter->export()->saveAs('/path/to/file.csv');

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$autoSize boolean Enable autosize for xlsx/xls export. humhub\components\export\SpreadsheetExport
$columns array|humhub\components\export\Column[] humhub\components\export\SpreadsheetExport
$dataProvider \yii\data\DataProviderInterface The data provider for the view. humhub\components\export\SpreadsheetExport
$emptyCell string The HTML display when the content of a cell is empty. humhub\components\export\SpreadsheetExport
$nullDisplay string The text to be displayed when formatting a null data value. humhub\components\export\SpreadsheetExport
$query \yii\db\QueryInterface The data source query. humhub\components\export\SpreadsheetExport
$resultConfig array Configuration for humhub\components\export\ExportResult instance created in process result. humhub\components\export\SpreadsheetExport
$showFooter boolean Whether to show the footer section of the sheet. humhub\components\export\SpreadsheetExport
$showHeader boolean Whether to show the header section of the sheet. humhub\components\export\SpreadsheetExport

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
applyAutoSize() Enable AutoSize for Export humhub\components\export\SpreadsheetExport
composeBodyRow() Composes body row contents. humhub\components\export\SpreadsheetExport
composeFooterRow() Composes header row contents. humhub\components\export\SpreadsheetExport
composeHeaderRow() Composes header row contents. humhub\components\export\SpreadsheetExport
createDataColumn() Creates a humhub\components\export\DataColumn object based on a string in the format of "attribute:format:label". humhub\components\export\SpreadsheetExport
gc() Performs PHP memory garbage collection. humhub\components\export\SpreadsheetExport
guessColumns() This function tries to guess the columns to show from the given data if $columns are not explicitly specified. humhub\components\export\SpreadsheetExport
initColumns() Creates column objects and initializes them. humhub\components\export\SpreadsheetExport
nextRow() humhub\components\export\SpreadsheetExport

Property Details

$autoSize public property

Enable autosize for xlsx/xls export.

public boolean $autoSize true
$columns public property
$dataProvider public property

The data provider for the view. This property can be omitted in case $query is set.

public \yii\data\DataProviderInterface $dataProvider null
$emptyCell public property

The HTML display when the content of a cell is empty. This property is used to render cells that have no defined content, e.g. empty footer or filter cells.

Note that this is not used by the humhub\components\export\DataColumn if a data item is null. In that case the $nullDisplay property will be used to indicate an empty data value.

public string $emptyCell ''
$nullDisplay public property

The text to be displayed when formatting a null data value.

public string $nullDisplay ''
$query public property

The data source query. Note: this field will be ignored in case $dataProvider is set.

public \yii\db\QueryInterface $query null
$resultConfig public property

Configuration for humhub\components\export\ExportResult instance created in process result.

For example:

[
    'forceArchive' => true
]

See also humhub\components\export\ExportResult.

public array $resultConfig = []
$showFooter public property

Whether to show the footer section of the sheet.

public boolean $showFooter false
$showHeader public property

Whether to show the header section of the sheet.

public boolean $showHeader true

Method Details

applyAutoSize() protected method

Enable AutoSize for Export

protected void applyAutoSize ( $spreadsheet )
$spreadsheet \PhpOffice\PhpSpreadsheet\Spreadsheet
throws \PhpOffice\PhpSpreadsheet\Exception
composeBodyRow() protected method

Composes body row contents.

protected void composeBodyRow ( $spreadsheet, $model, $key, $index )
$spreadsheet \PhpOffice\PhpSpreadsheet\Spreadsheet
$model mixed

The data model

$key mixed

The key associated with the data model

$index integer

Zero-based index of data model among the models array returned by \humhub\components\export\GridView::dataProvider.

throws \PhpOffice\PhpSpreadsheet\Exception
composeFooterRow() protected method

Composes header row contents.

protected void composeFooterRow ( $spreadsheet )
$spreadsheet \PhpOffice\PhpSpreadsheet\Spreadsheet
throws \PhpOffice\PhpSpreadsheet\Exception
composeHeaderRow() protected method

Composes header row contents.

protected void composeHeaderRow ( $spreadsheet )
$spreadsheet \PhpOffice\PhpSpreadsheet\Spreadsheet
throws \PhpOffice\PhpSpreadsheet\Exception
createDataColumn() protected method

Creates a humhub\components\export\DataColumn object based on a string in the format of "attribute:format:label".

protected humhub\components\export\DataColumn createDataColumn ( $text )
$text string

The column specification string

return humhub\components\export\DataColumn

The column instance

throws \yii\base\InvalidConfigException

if the column specification is invalid

export() public method

Performs data export.

public humhub\components\export\ExportResult export ( )
return humhub\components\export\ExportResult

Export result.

throws \yii\base\InvalidConfigException
throws \PhpOffice\PhpSpreadsheet\Exception
gc() protected method

Performs PHP memory garbage collection.

protected void gc ( )
getFormatter() public method

public \yii\i18n\Formatter getFormatter ( )
return \yii\i18n\Formatter

Formatter instance

throws \yii\base\InvalidConfigException
guessColumns() protected method

This function tries to guess the columns to show from the given data if $columns are not explicitly specified.

protected void guessColumns ( $model )
$model array

List of model

init() public method

Initializes the grid.

This method will initialize required property values and instantiate $columns objects.

public void init ( )
initColumns() protected method

Creates column objects and initializes them.

protected void initColumns ( $model )
$model array

List of single row model

throws \yii\base\InvalidConfigException
nextRow() protected method

protected integer nextRow ( )
setFormatter() public method

public void setFormatter ( $formatter )
$formatter array|\yii\i18n\Formatter