yii2tech/csv-grid
最新稳定版本:1.0.5
Composer 安装命令:
composer require yii2tech/csv-grid
包简介
Yii2 extension data export to CSV file
关键字:
README 文档
README
CSV Data Export extension for Yii2
This extension provides ability to export data to CSV file.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/csv-grid
or add
"yii2tech/csv-grid": "*"
to the require section of your composer.json.
Usage
This extension provides ability to export data to CSV file.
Export is performed via \yii2tech\csvgrid\CsvGrid instance, which provides interface similar to \yii\grid\GridView widget.
Example:
<?php use yii2tech\csvgrid\CsvGrid; use yii\data\ArrayDataProvider; $exporter = new CsvGrid([ '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');
\yii2tech\csvgrid\CsvGrid allows exporting of the \yii\data\DataProviderInterface and \yii\db\QueryInterface instances.
Export is performed via batches, which allows processing of the large data without memory overflow.
In case of \yii\data\DataProviderInterface usage, data will be split to batches using pagination mechanism.
Thus you should setup pagination with page size in order to control batch size:
<?php use yii2tech\csvgrid\CsvGrid; use yii\data\ActiveDataProvider; $exporter = new CsvGrid([ 'dataProvider' => new ActiveDataProvider([ 'query' => Item::find(), 'pagination' => [ 'pageSize' => 100, // export batch size ], ]), ]); $exporter->export()->saveAs('/path/to/file.csv');
Note: if you disable pagination in your data provider - no batch processing will be performed.
In case of \yii\db\QueryInterface usage, CsvGrid will attempt to use batch() method, if it present in the query
class (for example in case \yii\db\Query or \yii\db\ActiveQuery usage). If batch() method is not available -
yii\data\ActiveDataProvider instance will be automatically created around given query.
You can control batch size via \yii2tech\csvgrid\CsvGrid::$batchSize:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'batchSize' => 200, // export batch size ]); $exporter->export()->saveAs('/path/to/file.csv');
While running web application you can use \yii2tech\csvgrid\ExportResult::send() method to send a result file to
the browser through download dialog:
<?php use yii2tech\csvgrid\CsvGrid; use yii\data\ActiveDataProvider; use yii\web\Controller; class ItemController extends Controller { public function actionExport() { $exporter = new CsvGrid([ 'dataProvider' => new ActiveDataProvider([ 'query' => Item::find(), ]), ]); return $exporter->export()->send('items.csv'); } }
Splitting result into several files
While exporting large amount of data, you may want to split export results into several files. This may come in handy in case you are planning to use result CSV files with program, which have a limit on maximum rows inside single file. For example: 'Open Office' and 'MS Excel 97-2003' allows maximum 65536 rows per CSV file, 'MS Excel 2007' - 1048576.
You may use \yii2tech\csvgrid\CsvGrid::$maxEntriesPerFile to restrict maximum rows in the single result file.
In case the export result produce more then one CSV file - these files will be automatically archived into the single
archive file. For example:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'maxEntriesPerFile' => 60000, // limit max rows per single file ]); $exporter->export()->saveAs('/path/to/archive-file.zip'); // output ZIP archive!
Note: you are not forced to receive multiple files result as a single archive. You can use
\yii2tech\csvgrid\ExportResult::$csvFiles to manually iterate over created CSV files and process them as you like:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'maxEntriesPerFile' => 60000, // limit max rows per single file ]); $result = $exporter->export(); foreach ($result->csvFiles as $csvFile) { /* @var $csvFile \yii2tech\csvgrid\CsvFile */ copy($csvFile->name, '/path/to/dir/' . basename($csvFile->name)); }
Archiving results
Export result is archived automatically, if it contains more then one CSV file. However, you may enforce archiving of the
export result via \yii2tech\csvgrid\ExportResult::$forceArchive:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'resultConfig' => [ 'forceArchive' => true // always archive the results ], ]); $exporter->export()->saveAs('/path/to/archive-file.zip'); // output ZIP archive!
Heads up! By default \yii2tech\csvgrid\ExportResult uses PHP Zip extension for the archive creating.
Thus it will fail, if this extension is not present in your environment.
You can setup your own archive method via \yii2tech\csvgrid\ExportResult::$archiver.
For example:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'resultConfig' => [ 'forceArchive' => true, 'archiver' => function (array $files, $dirName) { $archiveFileName = $dirName . DIRECTORY_SEPARATOR . 'items.tar'; foreach ($files as $fileName) { // add $fileName to $archiveFileName archive } return $archiveFileName; }, ], ]); $exporter->export()->saveAs('/path/to/items.tar');
While sending file to the browser via \yii2tech\csvgrid\ExportResult::send() there is no need to check if result
is archived or not as correct file extension will be append automatically:
<?php use yii2tech\csvgrid\CsvGrid; use yii\data\ActiveDataProvider; use yii\web\Controller; class ItemController extends Controller { public function actionExport() { $exporter = new CsvGrid([ 'dataProvider' => new ActiveDataProvider([ 'query' => Item::find(), // over 1 million records ]), 'maxEntriesPerFile' => 60000, ]); return $exporter->export()->send('items.csv'); // displays dialog for saving `items.csv.zip`! } }
Customize output format
Although CSV dictates particular data format (each value quoted, values separated by comma, lines separated by line break),
some cases require its changing. For example: you may need to separate values using semicolon, or may want to create
TSV (tabular separated values) file instead CSV.
You may customize format entries using \yii2tech\csvgrid\CsvGrid::$csvFileConfig:
<?php use yii2tech\csvgrid\CsvGrid; $exporter = new CsvGrid([ 'query' => Item::find(), 'csvFileConfig' => [ 'cellDelimiter' => "\t", 'rowDelimiter' => "\n", 'enclosure' => '', ], ]); $exporter->export()->saveAs('/path/to/file.txt');
yii2tech/csv-grid 适用场景与选型建议
yii2tech/csv-grid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16M 次下载、GitHub Stars 达 85, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「export」 「yii2」 「large data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/csv-grid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/csv-grid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/csv-grid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bulk export of sylius resources
A custom URL rule class for Yii 2 which allows to create translated URL rules
Yii2 export extension
laravel facade to read/write csv file
A simple API extension for SplFileObject
Collection of tools to use the full power of the Enyalius framework
统计信息
- 总下载量: 1.16M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 87
- 点击次数: 15
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04