Revision #5 has been created by rackycz on Sep 8, 2020, 10:02:37 AM with the memo:
Composer and PhpExcel
« previous (#4) next (#6) »
Changes
Title
unchanged
Yii v2 snippet guide II
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2,snippets
Content
changed
[...]
var_dump($result);
```
**Creating models in Gii for remote MSSQL tables**
---
Once you have added the 2nd database (read above) go to the Model Generator in Gii. Change there the DB connection to whatever you named the connection in yii-config (in the example above it was "db2") and set tablename in format: SCHEMA.TBL_NAME. If MSSQL server has more databases, one of them is set to be the main DB. This will be used I think. I haven't succeeded to change the DB. DB can be set in the DSN string, but it had no effect in my case.
**Using composer-packages (libraries) - for example PhpExcel in Yii 2**
---
In previous chapters I showed how use PhpExcel in Yii 1. Now I needed it also in Yii 2 and it was extremely easy:
Note: [PhpExcel](https://github.com/PHPOffice/PHPExcel) was deprecated and is noe replaced with [PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet)
```
// 1) Command line:
// This downloads everything to folder "vendor"
composer require phpoffice/phpspreadsheet --prefer-source
// 2) PHP:
// Now you can directly use the package without any configuration:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Uncomment following rows if you want to set col width:
//$sheet->getColumnDimension('A')->setAutoSize(false);
//$sheet->getColumnDimension('A')->setWidth("50");
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
// will be saved in "web" folder
$writer->save('hello world.xlsx');
```