{"id":13507416,"url":"https://github.com/codemix/yii2-excelexport","last_synced_at":"2025-04-05T14:09:53.522Z","repository":{"id":50354998,"uuid":"57279948","full_name":"codemix/yii2-excelexport","owner":"codemix","description":"A utility to quickly create Excel files from query results or raw data","archived":false,"fork":false,"pushed_at":"2023-03-20T09:00:30.000Z","size":59,"stargazers_count":104,"open_issues_count":1,"forks_count":33,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-29T13:11:45.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-04-28T07:22:11.000Z","updated_at":"2025-03-15T20:58:20.000Z","dependencies_parsed_at":"2023-02-10T15:45:36.573Z","dependency_job_id":"75e6984e-45a5-4bb7-aaa3-e8810cd26233","html_url":"https://github.com/codemix/yii2-excelexport","commit_stats":{"total_commits":46,"total_committers":4,"mean_commits":11.5,"dds":"0.17391304347826086","last_synced_commit":"f25a1731314c1c4e0df5294ae05e5d5d71d3a55e"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-excelexport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-excelexport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-excelexport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-excelexport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/yii2-excelexport/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345856,"owners_count":20924102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:00:33.224Z","updated_at":"2025-04-05T14:09:53.501Z","avatar_url":"https://github.com/codemix.png","language":"PHP","funding_links":[],"categories":["Core components"],"sub_categories":[],"readme":"Yii2 Excel Export\n=================\n\n[![Latest Stable Version](https://poser.pugx.org/codemix/yii2-excelexport/v/stable)](https://packagist.org/packages/codemix/yii2-excelexport)\n[![Total Downloads](https://poser.pugx.org/codemix/yii2-excelexport/downloads)](https://packagist.org/packages/codemix/yii2-excelexport)\n[![Latest Unstable Version](https://poser.pugx.org/codemix/yii2-excelexport/v/unstable)](https://packagist.org/packages/codemix/yii2-excelexport)\n[![License](https://poser.pugx.org/codemix/yii2-excelexport/license)](https://packagist.org/packages/codemix/yii2-excelexport)\n\n\u003e **Note:** The minimum requirement since 2.6.0 is Yii 2.0.13. The latest\n\u003e version for older Yii releases is 2.5.0.\n\n## Features\n\n * Export data from `ActiveQuery` results\n * Export any other data (Array, Iterable, ...)\n * Create excel files with multiple sheets\n * Format cells and values\n\nTo write the Excel file, we use the excellent [PHPSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) package.\n\n## Installation\n\nInstall the package with [composer](http://getcomposer.org):\n\n    composer require codemix/yii2-excelexport\n\n## Quickstart example\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n        'Users' =\u003e [\n            'class' =\u003e 'codemix\\excelexport\\ActiveExcelSheet',\n            'query' =\u003e User::find(),\n        ]\n    ]\n]);\n$file-\u003esend('user.xlsx');\n```\n\nFind more examples below.\n\n\n## Configuration and Use\n\n### ExcelFile\n\nProperty | Description\n---------|-------------\n`writerClass` | The file format as supported by PHPOffice. The default is `\\PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx`\n`sheets` | An array of sheet configurations (see below). The keys are used as sheet names.\n`fileOptions` | Options to pass to the constructor of `mikehaertl\\tmp\\File`. Available keys are `prefix`, `suffix` and `directory`.\n\nMethods | Description\n---------|-------------\n`saveAs($name)` | Saves the excel file under `$name`\n`send($name=null, $inline=false, $contentType = 'application/vnd.ms-excel')` | Sends the excel file to the browser. If `$name` is empty, the file is streamed for inline display, otherwhise a download dialog will open, unless `$inline` is `true` which will force inline display even if a filename is supplied.\n`createSheets()` | Only creates the sheets of the excel workbook but does not save the file. This is usually called implicitely on `saveAs()` and `send()` but can also be called manually to modify the sheets before saving.\n`getWriter()` | Returns the `\\PhpOffice\\PhpSpreadsheet\\Writer\\BaseWrite` instance\n`getWorkbook()` | Returns the `\\PhpOffice\\PhpSpreadsheet\\Spreadsheet` workbook instance\n`getTmpFile()` | Returns the `mikehaertl\\tmp\\File` instance of the temporary file\n\n### ExcelSheet\n\nProperty | Description\n---------|-------------\n`data` | An array of data rows that should be used as sheet content\n`titles` (optional) | An array of column titles\n`types` (optional) | An array of types for specific columns as supported by PHPOffice, e.g. `DataType::TYPE_STRING`, indexed either by column name (e.g. `H`) or 0-based column index.\n`formats` (optional) | An array of format strings for specific columns as supported by Excel, e.g. `#,##0.00`, indexed either by column name (e.g. `H`) or 0-based column index.\n`formatters` (optional) | An array of value formatters for specific columns. Each must be a valid PHP callable whith the signature `function formatter($value, $row, $data)` where `$value` is the cell value to format, `$row` is the 0-based row index and `$data` is the current row data from the `data` configuration. The callbacks must be indexed either by column name (e.g. `H`) or by the 0-based column index.\n`styles` (optional) | An array of style configuration indexed by cell coordinates or a range.\n`callbacks` (optional) | An array of callbacks indexed by column that should be called after rendering a cell, e.g. to apply further complex styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PhpOffice\\PhpSpreadsheet\\Cell\\Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.\n`startColumn` (optional) | The start column name or its 0-based index. When this is set, the 0-based offset is added to all numeric keys used anywhere in this class. Columns referenced by name will stay unchanged.  Default is 'A'.\n`startRow` (optional) | The start row. Default is 1.\n\n\nEvent | Description\n---------|-------------\n`beforeRender` | Triggered before the sheet is rendered. The sheet is available via `$event-\u003esender-\u003egetSheet()`.\n`afterRender` | Triggered after the sheet was rendered. The sheet is available via `$event-\u003esender-\u003egetSheet()`.\n\n\n### ActiveExcelSheet\n\nThe class extends from `ExcelSheet` but differs in the following properties:\n\nProperty | Description\n---------|-------------\n`query` | The `ActiveQuery` for the row data (the `data` property will be ignored).\n`data` | The read-only property that returns the batched query result.\n`attributes` (optional) | The attributes to use as columns. Related attributes can be specifed in dot notation as usual, e.g. `team.name`. If not set, the `attributes()` from the corresponding `ActiveRecord` class will be used.\n`titles` (optional) | The column titles, indexed by column name (e.g. `H`) or 0-based column index. If a column is not listed here, the respective attribute label will be used. If set to `false` no title row will be rendered.\n`formats` (optional) | As in `ExcelSheet` but for `date`, `datetime` and `decimal` DB columns, the respective formats will be automatically set by default, according to the respective date format properties (see below) and the decimal precision.\n`formatters` (optional) | As in `ExcelSheet` but for `date` and `datetime` columns the value will be autoconverted to the correct excel time format with `\\PHPExcel_Shared_Date::PHPToExcel()` by default.\n`dateFormat` | The excel format to use for `date` DB types. Default is `dd/mm/yyyy`.\n`dateTimeFormat` | The excel format to use for `datetime` DB types. Default is `dd/mm/yyyy hh:mm:ss`.\n`batchSize` | The query batchsize to use. Default is `100`.\n`modelInstance` (optional) | The query's `modelClass` instance used to obtain attribute types and titles. If not set an instance of the query's `modelClass` is created automatically.\n\n\u003e **Note** Since version 2.3.1 datetime attributes will automatically be\n\u003e converted to the correct timezone. This feature makes use of the current\n\u003e [defaultTimeZone](http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#$defaultTimeZone-detail)\n\u003e and\n\u003e [timeZone](http://www.yiiframework.com/doc-2.0/yii-base-application.html#getTimeZone()-detail)\n\u003e setting of the app.\n\n## Examples\n\n### ActiveQuery results\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n\n    'writerClass' =\u003e '\\PhpOffice\\PhpSpreadsheet\\Writer\\Xls', // Override default of `\\PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx`\n\n    'sheets' =\u003e [\n\n        'Active Users' =\u003e [\n            'class' =\u003e 'codemix\\excelexport\\ActiveExcelSheet',\n            'query' =\u003e User::find()-\u003ewhere(['active' =\u003e true]),\n\n            // If not specified, all attributes from `User::attributes()` are used\n            'attributes' =\u003e [\n                'id',\n                'name',\n                'email',\n                'team.name',    // Related attribute\n                'created_at',\n            ],\n\n            // If not specified, the label from the respective record is used.\n            // You can also override single titles, like here for the above `team.name`\n            'titles' =\u003e [\n                'D' =\u003e 'Team Name',\n            ],\n        ],\n\n    ],\n]);\n$file-\u003esend('demo.xlsx');\n```\n\n### Raw data\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n\n        'Result per Country' =\u003e [   // Name of the excel sheet\n            'data' =\u003e [\n                ['fr', 'France', 1.234, '2014-02-03 12:13:14'],\n                ['de', 'Germany', 2.345, '2014-02-05 19:18:39'],\n                ['uk', 'United Kingdom', 3.456, '2014-03-03 16:09:04'],\n            ],\n\n            // Set to `false` to suppress the title row\n            'titles' =\u003e [\n                'Code',\n                'Name',\n                'Volume',\n                'Created At',\n            ],\n\n            'formats' =\u003e [\n                // Either column name or 0-based column index can be used\n                'C' =\u003e '#,##0.00',\n                3 =\u003e 'dd/mm/yyyy hh:mm:ss',\n            ],\n\n            'formatters' =\u003e [\n                // Dates and datetimes must be converted to Excel format\n                3 =\u003e function ($value, $row, $data) {\n                    return \\PhpOffice\\PhpSpreadsheet\\Shared\\Date::PHPToExcel(strtotime($value));\n                },\n            ],\n        ],\n\n        'Countries' =\u003e [\n            // Data for another sheet goes here ...\n        ],\n    ]\n]);\n// Save on disk\n$file-\u003esaveAs('/tmp/export.xlsx');\n```\n\n### Query builder results\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n\n        'Users' =\u003e [\n            'data' =\u003e new (\\yii\\db\\Query)\n                -\u003eselect(['id','name','email'])\n                -\u003efrom('user')\n                -\u003eeach(100);\n            'titles' =\u003e ['ID', 'Name', 'Email'],\n        ],\n    ]\n]);\n$file-\u003esend('demo.xlsx');\n```\n\n### Styling\n\nSince version 2.3.0 you can style single cells and cell ranges via the `styles`\nproperty of a sheet. For details on the accepted styling format please consult the\n[PhpSpreadsheet documentation](https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet-style.html).\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n        'Users' =\u003e [\n            'class' =\u003e 'codemix\\excelexport\\ActiveExcelSheet',\n            'query' =\u003e User::find(),\n            'styles' =\u003e [\n                'A1:Z1000' =\u003e [\n                    'font' =\u003e [\n                        'bold' =\u003e true,\n                        'color' =\u003e ['rgb' =\u003e 'FF0000'],\n                        'size' =\u003e 15,\n                        'name' =\u003e 'Verdana'\n                    ],\n                    'alignment' =\u003e [\n                        'horizontal' =\u003e Alignment::HORIZONTAL_RIGHT,\n                    ],\n                ],\n            ],\n        ]\n    ]\n]);\n```\n\nAs you have access to the `PHPExcel` object you can also \"manually\" modify the excel file as you like.\n\n\n```php\n\u003c?php\n// Create the actual workbook and sheets\n$file-\u003ecreateSheets();\n$file\n    -\u003egetWorkbook();\n    -\u003egetSheet(1)\n    -\u003egetStyle('B1')\n    -\u003egetFont()\n    -\u003egetColor()\n    -\u003esetARGB(\\PhpOffice\\PhpSpreadsheet\\Style\\Color::COLOR_RED);\n$file-\u003esend();\n```\n\nAlternatively you can also use the callback feature from our `ExcelSheet`:\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n        'Users' =\u003e [\n            'class' =\u003e 'codemix\\excelexport\\ActiveExcelSheet',\n            'query' =\u003e User::find(),\n            'callbacks' =\u003e [\n                // $cell is a \\PhpOffice\\PhpSpreadsheet\\Cell object\n                'A' =\u003e function ($cell, $row, $column) {\n                    $cell-\u003egetStyle()-\u003eapplyFromArray([\n                        'font' =\u003e [\n                            'bold' =\u003e true,\n                        ],\n                        'alignment' =\u003e [\n                            'horizontal' =\u003e \\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_RIGHT,\n                        ],\n                        'borders' =\u003e [\n                            'top' =\u003e [\n                                'style' =\u003e \\PhpOffice\\PhpSpreadsheet\\Style\\Border::BORDER_THIN,\n                            ],\n                        ],\n                        'fill' =\u003e [\n                            'fillType' =\u003e \\PhpOffice\\PhpSpreadsheet\\Style\\Fill::FILL_GRADIENT_LINEAR,\n                            'rotation' =\u003e 90,\n                            'startColor' =\u003e [\n                                'argb' =\u003e 'FFA0A0A0',\n                            ],\n                            'endColor' =\u003e [\n                                'argb' =\u003e 'FFFFFFFF',\n                            ],\n                        ],\n                    ]);\n                },\n            ],\n        ],\n    ],\n]);\n```\n\n### Events\n\nSince version 2.5.0 there are new events which make it easier to further modify each sheet.\n\n```php\n\u003c?php\n$file = \\Yii::createObject([\n    'class' =\u003e 'codemix\\excelexport\\ExcelFile',\n    'sheets' =\u003e [\n        'Users' =\u003e [\n            'class' =\u003e 'codemix\\excelexport\\ActiveExcelSheet',\n            'query' =\u003e User::find(),\n            'startRow' =\u003e 3,\n            'on beforeRender' =\u003e function ($event) {\n                $sheet = $event-\u003esender-\u003egetSheet();\n                $sheet-\u003esetCellValue('A1', 'List of current users');\n            }\n        ],\n    ],\n]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-excelexport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fyii2-excelexport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-excelexport/lists"}