1. Go to this page and download the library: Download yii2tech/csv-grid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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');
use yii2tech\csvgrid\CsvGrid;
$exporter = new CsvGrid([
'query' => Item::find(),
'batchSize' => 200, // export batch size
]);
$exporter->export()->saveAs('/path/to/file.csv');
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');
}
}
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!
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));
}
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!
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');
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`!
}
}