yii2-extensions / csv-export
Yii2 extension data export to CSV file
Package info
github.com/yii2-extensions/csv-export
Type:yii2-extension
pkg:composer/yii2-extensions/csv-export
Requires
- php: >=8.3
- yiisoft/yii2: ~2.0.55 || ^22.0@dev
Requires (Dev)
- infection/infection: ^0.35
- maglnet/composer-require-checker: ^4.1
- php-forge/coding-standard: ^0.3
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0.3
- phpunit/phpunit: ^12.5
- xepozz/internal-mocker: ^1.4
- yii2-extensions/phpstan: ^0.6
Suggests
- ext-zip: Required for the built-in ZIP archiver. Single CSV files and custom archivers can work without it.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 10:15:58 UTC
README
CSV Export
CSV data export for Yii2 data providers and queries
Batched reads, file splitting, ZIP archiving, spreadsheet-safe text, and download delivery
Export a data provider or a query to CSV through a grid-style column configuration. The exporter reads one batch at a time, can split the result into several files, and delivers it as a saved file or a browser download.
Features
Quick start
Installation
Requires PHP 8.3 or later. The built-in archiver needs the zip extension when an export produces several files.
composer require yii2-extensions/csv-export:^0.1
For development installs, follow the installation guide.
Usage
A sales schema: order has a number, a status, a total and a created_at timestamp, and references customer
(name, email) through a customer_id foreign key. Order declares the customer relation.
use yii2\extensions\csvexport\CsvExporter; use yii2\extensions\csvexport\SerialColumn; $exporter = new CsvExporter( [ // Read 500 orders at a time. with() loads the customers of each batch in one query. 'query' => Order::find() ->with('customer') ->where(['status' => 'paid']) ->orderBy(['id' => SORT_ASC]), 'batchSize' => 500, 'columns' => [ // A row number that keeps counting across batches and files. ['class' => SerialColumn::class], // Shorthand: attribute, attribute:format or attribute:format:label. // Without a label, the header is the attribute label of the model. 'number', 'created_at:datetime:Placed at', // A value read through the relation. ['attribute' => 'customer.name', 'header' => 'Customer'], // A computed value. [ 'header' => 'Contact', 'value' => static fn(Order $order): string => strtolower($order->customer->email), ], // A formatter format with arguments, and a footer cell. ['attribute' => 'total', 'format' => ['decimal', 2], 'footer' => 'Paid orders only'], ], 'showFooter' => true, // The limit counts the header and the footer: each file holds up to 49,998 orders and repeats both rows. 'maxEntriesPerFile' => 50000, 'csvFileConfig' => [ 'cellDelimiter' => ';', // A byte order mark for spreadsheet import. 'writeBom' => true, // An apostrophe before text that a spreadsheet would run as a formula, such as =HYPERLINK(...). 'spreadsheetSafe' => true, ], // Files are named orders-001.csv, orders-002.csv; several files are delivered as orders.zip. 'resultConfig' => ['fileBaseName' => 'orders'], ], );
export() returns an ExportResult that owns the temporary files until they are delivered:
use yii2\extensions\csvexport\ExportResult; // In a controller action: a download. One file is offered as orders.csv, // a split export as orders.csv.zip. The temporary files are removed after the response is sent. return $exporter->export()->send('orders.csv'); // In a console command: publish to a path. A failed delivery returns false // and leaves the result in place, so it can be saved somewhere else. $result = $exporter->export(); // Archive even when the orders fit in one file, so the destination is always a ZIP. $result->forceArchive = true; if (!$result->saveAs('/reports/orders.zip')) { $result->saveAs('/backup/orders.zip'); } // In a maintenance task: remove the workspaces that interrupted exports left behind more than a day ago. ExportResult::purgeExpired('@runtime/csv-export', 86400);
Data providers, custom archivers and tab-separated output are in the Usage Examples. The Configuration Guide details every option, the CSV dialect and the cleanup of temporary files.
Documentation
Practical guides and complete examples for batched exports, CSV output and delivery.
- ๐ Installation Guide: requirements, development installation and the runtime directory.
- โ๏ธ Configuration Guide: data sources, columns, splitting, CSV dialect and temporary files.
- ๐ก Usage Examples: providers, queries, downloads, archives and cleanup.
- ๐งช Testing Guide: development checks.
The guides and the API reference generated
from the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/csv-export/.
AI assistance
AI agents: read the documentation index and fetch the Markdown version
of a page by appending index.md to its URL. The
AI agents guide covers skill installation, client setup and
verification.
- ๐ค
yii2-csv-exportskill: batched CSV export from data providers and queries, CSV dialect and spreadsheet policy, archiving, delivery and workspace cleanup. - ๐งญ
yii2-frameworkskill: Yii 22.0 application conventions.
The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.
Package information
Project status
Security
Built with AI
Our social networks
Credits
Inspired by yii2tech/csv-grid by Paul Klimov.