1. Go to this page and download the library: Download codemix/yii2-excelexport 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/ */
$file = \Yii::createObject([
'class' => 'codemix\excelexport\ExcelFile',
'writerClass' => '\PhpOffice\PhpSpreadsheet\Writer\Xls', // Override default of `\PhpOffice\PhpSpreadsheet\Writer\Xlsx`
'sheets' => [
'Active Users' => [
'class' => 'codemix\excelexport\ActiveExcelSheet',
'query' => User::find()->where(['active' => true]),
// If not specified, all attributes from `User::attributes()` are used
'attributes' => [
'id',
'name',
'email',
'team.name', // Related attribute
'created_at',
],
// If not specified, the label from the respective record is used.
// You can also override single titles, like here for the above `team.name`
'titles' => [
'D' => 'Team Name',
],
],
],
]);
$file->send('demo.xlsx');
$file = \Yii::createObject([
'class' => 'codemix\excelexport\ExcelFile',
'sheets' => [
'Result per Country' => [ // Name of the excel sheet
'data' => [
['fr', 'France', 1.234, '2014-02-03 12:13:14'],
['de', 'Germany', 2.345, '2014-02-05 19:18:39'],
['uk', 'United Kingdom', 3.456, '2014-03-03 16:09:04'],
],
// Set to `false` to suppress the title row
'titles' => [
'Code',
'Name',
'Volume',
'Created At',
],
'formats' => [
// Either column name or 0-based column index can be used
'C' => '#,##0.00',
3 => 'dd/mm/yyyy hh:mm:ss',
],
'formatters' => [
// Dates and datetimes must be converted to Excel format
3 => function ($value, $row, $data) {
return \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel(strtotime($value));
},
],
],
'Countries' => [
// Data for another sheet goes here ...
],
]
]);
// Save on disk
$file->saveAs('/tmp/export.xlsx');