Search by

yii2-extensions / csv-export

terabytesoftw

Yii2 extension data export to CSV file

Package info

github.com/yii2-extensions/csv-export

Forum

Documentation

Type:yii2-extension

pkg:composer/yii2-extensions/csv-export

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

0.1.0 2026-11-29 14:48 UTC

This package is auto-updated.

Last update: 2026-11-30 10:15:58 UTC


README

Yii Framework

CSV Export


PHPUnit Coverage Mutation Testing PHPStan

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

Feature Overview

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-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.

Package information

PHP Yii 2.0.55+ Yii 22.0.x

Project status

PHPStan Level Max Quality StyleCI

Security

OpenSSF Scorecard Zizmor

Built with AI

Built with Claude Code Built with Codex Reviewed by CodeRabbit

Our social networks

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

Credits

Inspired by yii2tech/csv-grid by Paul Klimov.

License

License