1. Go to this page and download the library: Download yii2tech/spreadsheet 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\spreadsheet\Spreadsheet;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
class ItemController extends Controller
{
public function actionExport()
{
$exporter = new Spreadsheet([
'dataProvider' => new ActiveDataProvider([
'query' => Item::find(),
]),
]);
return $exporter->send('items.xls');
}
}
use yii2tech\spreadsheet\Spreadsheet;
use yii\data\ActiveDataProvider;
use app\models\Equipment;
$exporter = (new Spreadsheet([
'title' => 'Monitors',
'dataProvider' => new ActiveDataProvider([
'query' => Equipment::find()->andWhere(['group' => 'monitor']),
]),
'columns' => [
[
'attribute' => 'name',
],
[
'attribute' => 'price',
],
],
]))->render(); // call `render()` to create a single worksheet
$exporter->configure([ // update spreadsheet configuration
'title' => 'Mouses',
'dataProvider' => new ActiveDataProvider([
'query' => Equipment::find()->andWhere(['group' => 'mouse']),
]),
])->render(); // call `render()` to create a single worksheet
$exporter->configure([ // update spreadsheet configuration
'title' => 'Keyboards',
'dataProvider' => new ActiveDataProvider([
'query' => Equipment::find()->andWhere(['group' => 'keyboard']),
]),
])->render(); // call `render()` to create a single worksheet
$exporter->save('/path/to/file.xls');
use yii2tech\spreadsheet\Spreadsheet;
use yii\data\ActiveDataProvider;
$exporter = new Spreadsheet([
'dataProvider' => new ActiveDataProvider([
'query' => Item::find(),
'pagination' => [
'pageSize' => 100, // export batch size
],
]),
]);
$exporter->save('/path/to/file.xls');
use yii2tech\spreadsheet\Spreadsheet;
use yii\data\ActiveDataProvider;
$exporter = new Spreadsheet([
'query' => Item::find(),
'batchSize' => 200, // export batch size
]);
$exporter->save('/path/to/file.xls');