{"id":15169490,"url":"https://github.com/yii2tech/csv-grid","last_synced_at":"2025-10-01T02:31:14.234Z","repository":{"id":57087041,"uuid":"60169416","full_name":"yii2tech/csv-grid","owner":"yii2tech","description":"Yii2 extension for CSV export","archived":true,"fork":false,"pushed_at":"2020-03-04T11:17:40.000Z","size":52,"stargazers_count":91,"open_issues_count":0,"forks_count":19,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-04T07:03:23.791Z","etag":null,"topics":["csv","export","yii","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yii2tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["klimov-paul"],"patreon":"klimov_paul"}},"created_at":"2016-06-01T10:58:03.000Z","updated_at":"2023-12-26T13:54:50.000Z","dependencies_parsed_at":"2022-08-20T15:31:12.094Z","dependency_job_id":null,"html_url":"https://github.com/yii2tech/csv-grid","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fcsv-grid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fcsv-grid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fcsv-grid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fcsv-grid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2tech","download_url":"https://codeload.github.com/yii2tech/csv-grid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219875269,"owners_count":16554660,"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":["csv","export","yii","yii2","yii2-extension"],"created_at":"2024-09-27T07:02:09.002Z","updated_at":"2025-10-01T02:31:13.923Z","avatar_url":"https://github.com/yii2tech.png","language":"PHP","funding_links":["https://github.com/sponsors/klimov-paul","https://patreon.com/klimov_paul"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yii2tech\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars2.githubusercontent.com/u/12951949\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eCSV Data Export extension for Yii2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThis extension provides ability to export data to CSV file.\n\nFor license information check the [LICENSE](LICENSE.md)-file.\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/yii2tech/csv-grid.svg)](https://packagist.org/packages/yii2tech/csv-grid)\n[![Total Downloads](https://img.shields.io/packagist/dt/yii2tech/csv-grid.svg)](https://packagist.org/packages/yii2tech/csv-grid)\n[![Build Status](https://travis-ci.org/yii2tech/csv-grid.svg?branch=master)](https://travis-ci.org/yii2tech/csv-grid)\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2tech/csv-grid\n```\n\nor add\n\n```json\n\"yii2tech/csv-grid\": \"*\"\n```\n\nto the require section of your composer.json.\n\n\nUsage\n-----\n\nThis extension provides ability to export data to CSV file.\nExport is performed via `\\yii2tech\\csvgrid\\CsvGrid` instance, which provides interface similar to `\\yii\\grid\\GridView` widget.\n\nExample:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\nuse yii\\data\\ArrayDataProvider;\n\n$exporter = new CsvGrid([\n    'dataProvider' =\u003e new ArrayDataProvider([\n        'allModels' =\u003e [\n            [\n                'name' =\u003e 'some name',\n                'price' =\u003e '9879',\n            ],\n            [\n                'name' =\u003e 'name 2',\n                'price' =\u003e '79',\n            ],\n        ],\n    ]),\n    'columns' =\u003e [\n        [\n            'attribute' =\u003e 'name',\n        ],\n        [\n            'attribute' =\u003e 'price',\n            'format' =\u003e 'decimal',\n        ],\n    ],\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/file.csv');\n```\n\n`\\yii2tech\\csvgrid\\CsvGrid` allows exporting of the `\\yii\\data\\DataProviderInterface` and `\\yii\\db\\QueryInterface` instances.\nExport is performed via batches, which allows processing of the large data without memory overflow.\n\nIn case of `\\yii\\data\\DataProviderInterface` usage, data will be split to batches using pagination mechanism.\nThus you should setup pagination with page size in order to control batch size:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\nuse yii\\data\\ActiveDataProvider;\n\n$exporter = new CsvGrid([\n    'dataProvider' =\u003e new ActiveDataProvider([\n        'query' =\u003e Item::find(),\n        'pagination' =\u003e [\n            'pageSize' =\u003e 100, // export batch size\n        ],\n    ]),\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/file.csv');\n```\n\n\u003e Note: if you disable pagination in your data provider - no batch processing will be performed.\n\nIn case of `\\yii\\db\\QueryInterface` usage, `CsvGrid` will attempt to use `batch()` method, if it present in the query\nclass (for example in case `\\yii\\db\\Query` or `\\yii\\db\\ActiveQuery` usage). If `batch()` method is not available -\n`yii\\data\\ActiveDataProvider` instance will be automatically created around given query.\nYou can control batch size via `\\yii2tech\\csvgrid\\CsvGrid::$batchSize`:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'batchSize' =\u003e 200, // export batch size\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/file.csv');\n```\n\nWhile running web application you can use `\\yii2tech\\csvgrid\\ExportResult::send()` method to send a result file to\nthe browser through download dialog:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\nuse yii\\data\\ActiveDataProvider;\nuse yii\\web\\Controller;\n\nclass ItemController extends Controller\n{\n    public function actionExport()\n    {\n        $exporter = new CsvGrid([\n            'dataProvider' =\u003e new ActiveDataProvider([\n                'query' =\u003e Item::find(),\n            ]),\n        ]);\n        return $exporter-\u003eexport()-\u003esend('items.csv');\n    }\n}\n```\n\n\n## Splitting result into several files \u003cspan id=\"splitting-result-into-several-files\"\u003e\u003c/span\u003e\n\nWhile exporting large amount of data, you may want to split export results into several files.\nThis may come in handy in case you are planning to use result CSV files with program, which have a limit on\nmaximum rows inside single file. For example: 'Open Office' and 'MS Excel 97-2003' allows maximum 65536 rows\nper CSV file, 'MS Excel 2007' - 1048576.\n\nYou may use `\\yii2tech\\csvgrid\\CsvGrid::$maxEntriesPerFile` to restrict maximum rows in the single result file.\nIn case the export result produce more then one CSV file - these files will be automatically archived into the single\narchive file. For example:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'maxEntriesPerFile' =\u003e 60000, // limit max rows per single file\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/archive-file.zip'); // output ZIP archive!\n```\n\nNote: you are not forced to receive multiple files result as a single archive. You can use\n`\\yii2tech\\csvgrid\\ExportResult::$csvFiles` to manually iterate over created CSV files and process them as you like:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'maxEntriesPerFile' =\u003e 60000, // limit max rows per single file\n]);\n$result = $exporter-\u003eexport();\n\nforeach ($result-\u003ecsvFiles as $csvFile) {\n    /* @var $csvFile \\yii2tech\\csvgrid\\CsvFile */\n    copy($csvFile-\u003ename, '/path/to/dir/' . basename($csvFile-\u003ename));\n}\n```\n\n\n## Archiving results \u003cspan id=\"archiving-results\"\u003e\u003c/span\u003e\n\nExport result is archived automatically, if it contains more then one CSV file. However, you may enforce archiving of the\nexport result via `\\yii2tech\\csvgrid\\ExportResult::$forceArchive`:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'resultConfig' =\u003e [\n        'forceArchive' =\u003e true // always archive the results\n    ],\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/archive-file.zip'); // output ZIP archive!\n```\n\n**Heads up!** By default `\\yii2tech\\csvgrid\\ExportResult` uses [PHP Zip](http://php.net/manual/en/book.zip.php) extension for the archive creating.\nThus it will fail, if this extension is not present in your environment.\n\nYou can setup your own archive method via `\\yii2tech\\csvgrid\\ExportResult::$archiver`.\nFor example:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'resultConfig' =\u003e [\n        'forceArchive' =\u003e true,\n        'archiver' =\u003e function (array $files, $dirName) {\n            $archiveFileName = $dirName . DIRECTORY_SEPARATOR . 'items.tar';\n\n            foreach ($files as $fileName) {\n                // add $fileName to $archiveFileName archive\n            }\n\n            return $archiveFileName;\n        },\n    ],\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/items.tar');\n```\n\nWhile sending file to the browser via `\\yii2tech\\csvgrid\\ExportResult::send()` there is no need to check if result\nis archived or not as correct file extension will be append automatically:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\nuse yii\\data\\ActiveDataProvider;\nuse yii\\web\\Controller;\n\nclass ItemController extends Controller\n{\n    public function actionExport()\n    {\n        $exporter = new CsvGrid([\n            'dataProvider' =\u003e new ActiveDataProvider([\n                'query' =\u003e Item::find(), // over 1 million records\n            ]),\n            'maxEntriesPerFile' =\u003e 60000,\n        ]);\n\n        return $exporter-\u003eexport()-\u003esend('items.csv'); // displays dialog for saving `items.csv.zip`!\n    }\n}\n```\n\n\n## Customize output format \u003cspan id=\"customize-output-format\"\u003e\u003c/span\u003e\n\nAlthough CSV dictates particular data format (each value quoted, values separated by comma, lines separated by line break),\nsome cases require its changing. For example: you may need to separate values using semicolon, or may want to create\nTSV (tabular separated values) file instead CSV.\nYou may customize format entries using `\\yii2tech\\csvgrid\\CsvGrid::$csvFileConfig`:\n\n```php\n\u003c?php\n\nuse yii2tech\\csvgrid\\CsvGrid;\n\n$exporter = new CsvGrid([\n    'query' =\u003e Item::find(),\n    'csvFileConfig' =\u003e [\n        'cellDelimiter' =\u003e \"\\t\",\n        'rowDelimiter' =\u003e \"\\n\",\n        'enclosure' =\u003e '',\n    ],\n]);\n$exporter-\u003eexport()-\u003esaveAs('/path/to/file.txt');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fcsv-grid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2tech%2Fcsv-grid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fcsv-grid/lists"}