İçeriğe geç
PHP Ecosystem
Explore

CSV Export

Bu içerik henüz dilinizde mevcut değil.

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.

Feature Overview

Requires PHP 8.3 or later. The built-in archiver needs the zip extension when an export produces several files.

Terminal window
composer require yii2-extensions/csv-export:^0.1

For development installs, follow the installation guide.

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.

Practical guides and complete examples for batched exports, CSV output and delivery.

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 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-export skill: batched CSV export from data providers and queries, CSV dialect and spreadsheet policy, archiving, delivery and workspace cleanup.
  • 🧭 yii2-framework skill: 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.

Follow on X Follow on Facebook Join our Subreddit Join on Telegram

Inspired by yii2tech/csv-grid by Paul Klimov.

License