CakePHP Excel plugin
, (*1)
CakePHP Excel plugin allows for spreadsheet files manipulation with the power of CakePHP ORM.
This plugin is build using PHPSpreadsheet library and can work with multiple types of spreadsheet files (excel, csv etc)., (*2)
Installation
composer require robotusers/cakephp-excel
//Application.php
public function bootstrap()
{
...
$this->addPlugin('Robotusers/Excel');
}
Using the plugin
Excel plugin lets you manipulate spreadsheet files multiple ways. The simplest use case is to load your spreadhseet data into CakePHP ORM table., (*3)
For example we are loading an excel file that contains some record data., (*4)
|
A |
B |
C |
1 |
Led Zeppelin |
Led Zeppelin II |
1969 |
2 |
Deep Purple |
Machine Head |
1972 |
3 |
Pink Floyd |
Wish You Were Here |
1975 |
use Robotusers/Excel/Registry;
$registry = Registry::instance();
$table = $registry->get('path/to/records.xlsx', 'Albums');
Spreadsheet data is now loaded into CakePHP ORM table., (*5)
$row = $table->find()->first()->toArray();
//this is how a simple row looks like:
[
'_row' => 1,
'A' => 'Led Zeppelin',
'B' => 'Led Zeppelin II',
'C' => '1969'
]
Each column is represented as a property. Values are string
by default., (*6)
You may also map columns to custom properties and types., (*7)
use Robotusers/Excel/Registry;
$registry = Registry::instance();
$table = $registry->get('path/to/records.xlsx', 'Albums', [
'primaryKey' => 'id',
'columnMap' => [
'A' => 'band',
'B' => 'album',
'C' => 'year'
],
'columnTypeMap' => [
'C' => 'date'
]
]);
Spreadsheet data is now loaded into CakePHP ORM with custom properties and types., (*8)
$row = $table->find()->first()->toArray();
//this is how a simple row looks like:
[
'id' => 1,
'band' => 'Led Zeppelin',
'album' => 'Led Zeppelin II',
'year' => object(Cake\I18n\Date) {
'time' => '1969-01-01T00:00:00+00:00',
'timezone' => 'UTC'
}
]
You may want to manipulate some data and write it back to excel file. This is also possible., (*9)
$row = $table->newEntity([
'band' => 'Genesis',
'album' => 'Selling England by the Pound',
'year' => '1973'
]);
$table->save($row);
Now the new record is saved, but excel file has not been updated yet. You have to call writeSpreadsheet()
method:, (*10)
$table->writeSpreadsheet();
You may also want to read or write only some of the rows and columns., (*11)
use Robotusers/Excel/Registry;
$table = $registry->get('path/to/records.xlsx', 'Albums', [
'startRow' => 2,
'endRow' => 3,
'startColumn' => 'B',
'endColumn' => 'B'
]);
$row = $table->find()->first()->toArray();
//this is how a simple row looks like:
[
'_row' => 1,
'B' => 'Machine Head'
]
Note that _row
does not match the real row index. To keep original row indexes you must use keepOriginalRows
option., (*12)
use Robotusers/Excel/Registry;
$table = $registry->get('path/to/records.xlsx', 'Albums', [
'startRow' => 2,
'endRow' => 3,
'startColumn' => 'B',
'endColumn' => 'B',
'keepOriginalRows' => true
]);
$row = $table->find()->first()->toArray();
//this is how a simple row looks like:
[
'_row' => 2,
'B' => 'Machine Head'
]
The same principle applies to writing to a file. If you delete the second row it won't become empty in result excel file when keepOriginalRows
is false
. You have to set this option to true
if you want to keep rows consistency across the table and the file., (*13)
Behavior
This plugin provides a behavior which could be added to any table., (*14)
//AlbumsTable.php
public function initialize()
{
$this->addBehavior('Robotusers/Excel.Excel', [
'columnMap' => [
'A' => 'band',
'B' => 'album',
'C' => 'year'
]
]);
}
If you want to load data into your table you have to set a worksheet instance., (*15)
use Cake\Filesystem\File;
$file = new File('path/to/file.xls');
$spreadsheet = $table->getManager()->getSpreadsheet($file); // \PhpOffice\PhpSpreadsheet\Spreadsheet instance
$worksheet = $spreadsheet->getActiveSheet(); // \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet instance
$table->setWorksheet($worksheet)->readSpreadsheet();
Now your table is populated with excel data., (*16)
If you want to write your data back to excel file you have to set a file., (*17)
$table->setFile($file)->writeSpreadsheet();
Working with different tables
It is also possible to load data into any table., (*18)
use Robotusers\Excel\Excel\Manager;
$table = TableRegistry::get('SomeTable');
$manager = new Manager();
$file = new File('file.xlsx');
$spreadsheet = $manager->getSpreadsheet($file);
$worksheet = $spreadsheet->getActiveSheet();
$manager->read($worksheet, $table, [
'columnMap' => [
'A' => 'band',
'B' => 'album',
'C' => 'year'
]
]);
//manipulate your data...
//here you have to tell where properties should be placed
$manager->write($table, $worksheet, [
'propertyMap' => [
'band' => 'A',
'album' => 'B',
'year' => 'C'
]
]);
//to actually save the file you have to call save()
$writer = $manager->save($spreadsheet, $file);