Skip to content

Commit 83ea044

Browse files
Merge pull request #6 from robotusers/spreadsheet
Support phpoffice/phpspreadsheet
2 parents a00bf81 + 279c62e commit 83ea044

13 files changed

Lines changed: 282 additions & 233 deletions

File tree

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
[![codecov](https://codecov.io/gh/robotusers/cakephp-excel/branch/master/graph/badge.svg)](https://codecov.io/gh/robotusers/cakephp-excel)
88

99
CakePHP Excel plugin allows for spreadsheet files manipulation with the power of CakePHP ORM.
10-
This plugin is build using [PHPExcel](https://github.com/PHPOffice/PHPExcel) library and can work with multiple types of spreadsheet files (excel, csv etc).
10+
This plugin is build using [PHPSpreadsheet](https://github.com/PHPOffice/PHPSpreadsheet) library and can work with multiple types of spreadsheet files (excel, csv etc).
11+
12+
**NOTE:**
13+
CakePHP Excel plugin versions 0.4.0 and lower used now abandoned [PHPExcel](https://github.com/PHPOffice/PHPExcel) library.
1114

1215
## Installation
1316

@@ -22,11 +25,11 @@ Excel plugin lets you manipulate spreadsheet files multiple ways. The simplest u
2225

2326
For example we are loading an excel file that contains some record data.
2427

25-
| | A | B | C |
26-
|:--|:------------: |:------------------:| :---:|
27-
| 1 | Led Zeppelin | Led Zeppelin II | 1969 |
28-
| 2 | Deep Purple | Machine Head | 1972 |
29-
| 3 | Pink Floyd | Wish You Were Here | 1975 |
28+
| | A | B | C |
29+
|:--|:------------: |:---------------------:| :---:|
30+
| 1 | Led Zeppelin | Led Zeppelin II | 1969 |
31+
| 2 | Deep Purple | Machine Head | 1972 |
32+
| 3 | Pink Floyd | Wish You Were Here | 1975 |
3033

3134
```php
3235
use Robotusers/Excel/Registry;
@@ -100,10 +103,10 @@ $row = $table->newEntity([
100103
$table->save($row);
101104
```
102105

103-
Now the new record is saved, but excel file has not been updated yet. You have to call `writeExcel()` method:
106+
Now the new record is saved, but excel file has not been updated yet. You have to call `writeSpreadsheet()` method:
104107

105108
```php
106-
$table->writeExcel();
109+
$table->writeSpreadsheet();
107110
```
108111

109112
You may also want to read or write only some of the rows and columns.
@@ -176,18 +179,18 @@ If you want to load data into your table you have to set a worksheet instance.
176179
use Cake\Filesystem\File;
177180

178181
$file = new File('path/to/file.xls');
179-
$excel = $table->getManager()->getExcel($file); // PHPExcel instance
180-
$worksheet = $excel->getActiveSheet(); // PHPExcel_Worksheet instance
182+
$spreadsheet = $table->getManager()->getSpreadsheet($file); // \PhpOffice\PhpSpreadsheet\Spreadsheet instance
183+
$worksheet = $spreadsheet->getActiveSheet(); // \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet instance
181184

182-
$table->setWorksheet($worksheet)->readExcel();
185+
$table->setWorksheet($worksheet)->readSpreadsheet();
183186
```
184187

185188
Now your table is populated with excel data.
186189

187190
If you want to write your data back to excel file you have to set a file.
188191

189192
```php
190-
$table->setFile($file)->writeExcel();
193+
$table->setFile($file)->writeSpreadsheet();
191194
```
192195

193196
## Working with different tables
@@ -201,8 +204,8 @@ $table = TableRegistry::get('SomeTable');
201204
$manager = new Manager();
202205

203206
$file = new File('file.xlsx');
204-
$excel = $manager->getExcel($file);
205-
$worksheet = $excel->getActiveSheet();
207+
$spreadsheet = $manager->getSpreadsheet($file);
208+
$worksheet = $spreadsheet->getActiveSheet();
206209

207210
$manager->read($worksheet, $table, [
208211
'columnMap' => [
@@ -223,5 +226,5 @@ $manager->write($table, $worksheet, [
223226
]
224227
]);
225228
//to actually save the file you have to call save()
226-
$writer = $manager->save($excel, $file);
229+
$writer = $manager->save($spreadsheet, $file);
227230
```

composer.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"name": "robotusers/cakephp-excel",
3-
"description": "Robotusers CakePHP Excel plugin",
4-
"homepage": "https://github.com/robotusers/cakephp-excel",
5-
"type": "cakephp-plugin",
6-
"license": "MIT",
7-
"require": {
8-
"php": ">=5.5",
9-
"cakephp/filesystem": "^3.4",
10-
"cakephp/orm": "^3.4",
11-
"phpoffice/phpexcel": "^1.8"
12-
},
13-
"require-dev": {
14-
"cakephp/cakephp": "~3.4.0",
15-
"cakephp/cakephp-codesniffer": "*",
16-
"phpunit/phpunit": "^5.6|^6"
17-
},
18-
"autoload": {
19-
"psr-4": {
20-
"Robotusers\\Excel\\": "src"
21-
}
22-
},
23-
"autoload-dev": {
24-
"psr-4": {
25-
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
26-
"Robotusers\\Excel\\Test\\": "tests"
27-
}
28-
}
2+
"name": "robotusers/cakephp-excel",
3+
"description": "Robotusers CakePHP Excel plugin",
4+
"homepage": "https://github.com/robotusers/cakephp-excel",
5+
"type": "cakephp-plugin",
6+
"license": "MIT",
7+
"require": {
8+
"php": ">=5.5",
9+
"cakephp/filesystem": "^3.4",
10+
"cakephp/orm": "^3.4",
11+
"phpoffice/phpspreadsheet": "^1.1"
12+
},
13+
"require-dev": {
14+
"cakephp/cakephp": "~3.4.0",
15+
"cakephp/cakephp-codesniffer": "*",
16+
"phpunit/phpunit": "^5.6|^6"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Robotusers\\Excel\\": "src"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
26+
"Robotusers\\Excel\\Test\\": "tests"
27+
}
28+
}
2929
}

config/bootstrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
/*
2+
/*
33
* The MIT License
44
*
55
* Copyright 2017 RobotUsers
@@ -32,7 +32,7 @@
3232
$name = Registry::CONNECTON_NAME;
3333
$hasConnectionConfig = ConnectionManager::config($name);
3434
if (!$hasConnectionConfig && !in_array('sqlite', PDO::getAvailableDrivers())) {
35-
$msg = 'Excel not enabled. You need to either install pdo_sqlite, or define the "%s" connection name.';
35+
$msg = 'Spreadsheet not enabled. You need to either install pdo_sqlite, or define the "%s" connection name.';
3636
Log::warning(sprintf($msg, $name));
3737
return;
3838
}
@@ -45,4 +45,4 @@
4545
'cacheMetadata' => true,
4646
'quoteIdentifiers' => true,
4747
]);
48-
}
48+
}

src/Database/Factory.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
use Cake\Database\Schema\TableSchema;
3030
use Cake\Utility\Inflector;
3131
use Cake\Utility\Text;
32-
use PHPExcel_Cell;
33-
use PHPExcel_Cell_DataType;
34-
use PHPExcel_Worksheet;
35-
use PHPExcel_Worksheet_Row;
32+
use PhpOffice\PhpSpreadsheet\Cell\Cell;
33+
use PhpOffice\PhpSpreadsheet\Cell\DataType;
34+
use PhpOffice\PhpSpreadsheet\Worksheet\Row;
35+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
3636

3737
/**
3838
* Description of Factory
@@ -47,7 +47,7 @@ class Factory
4747
* @var array
4848
*/
4949
protected $dataTypeMap = [
50-
PHPExcel_Cell_DataType::TYPE_NULL => false
50+
DataType::TYPE_NULL => false
5151
];
5252

5353
/**
@@ -64,11 +64,11 @@ class Factory
6464

6565
/**
6666
*
67-
* @param PHPExcel_Worksheet $worksheet
67+
* @param Worksheet $worksheet
6868
* @param array $options
6969
* @return TableSchema
7070
*/
71-
public function createSchema(PHPExcel_Worksheet $worksheet, array $options = [])
71+
public function createSchema(Worksheet $worksheet, array $options = [])
7272
{
7373
$options += [
7474
'tableName' => $this->getTableName($worksheet),
@@ -96,10 +96,10 @@ public function createSchema(PHPExcel_Worksheet $worksheet, array $options = [])
9696
'columns' => [$primaryKey]
9797
]);
9898

99-
$row = new PHPExcel_Worksheet_Row($worksheet, $options['startRow']);
99+
$row = new Row($worksheet, $options['startRow']);
100100
$cells = $row->getCellIterator($options['startColumn'], $options['endColumn']);
101101
foreach ($cells as $cell) {
102-
/* @var $cell PHPExcel_Cell */
102+
/* @var $cell Cell */
103103

104104
$type = $this->discoverType($cell, $options);
105105

@@ -117,11 +117,11 @@ public function createSchema(PHPExcel_Worksheet $worksheet, array $options = [])
117117

118118
/**
119119
*
120-
* @param PHPExcel_Cell $cell
120+
* @param Cell $cell
121121
* @param array $options
122-
* @return array|string|false
122+
* @return array|string|bool
123123
*/
124-
protected function discoverType(PHPExcel_Cell $cell, array $options)
124+
protected function discoverType(Cell $cell, array $options)
125125
{
126126
$format = $cell->getStyle()->getNumberFormat()->getFormatCode();
127127
$dataType = $cell->getDataType();
@@ -159,16 +159,17 @@ public function createTable(Connection $connection, TableSchema $schema)
159159

160160
/**
161161
*
162-
* @param PHPExcel_Worksheet $worksheet
162+
* @param Worksheet $worksheet
163163
* @return string
164164
*/
165-
public function getTableName(PHPExcel_Worksheet $worksheet)
165+
public function getTableName(Worksheet $worksheet)
166166
{
167167
$excel = $worksheet->getParent();
168168
$title = $excel->getID() . ' ' . $worksheet->getTitle();
169169

170170
$slug = Text::slug($title, [
171-
'replacement' => '_'
171+
'replacement' => '_',
172+
'preserve' => '.',
172173
]);
173174
$camelized = Inflector::camelize($slug);
174175
$name = Inflector::tableize($camelized);
@@ -197,7 +198,7 @@ public function getNumberFormatMap()
197198
/**
198199
*
199200
* @param string $type
200-
* @param string|array|false $column
201+
* @param string|array|bool $column
201202
* @return $this
202203
*/
203204
public function setDataType($type, $column)
@@ -210,7 +211,7 @@ public function setDataType($type, $column)
210211
/**
211212
*
212213
* @param string $format
213-
* @param string|array|false $column
214+
* @param string|array|bool $column
214215
* @return $this
215216
*/
216217
public function setNumberFormat($format, $column)

0 commit comments

Comments
 (0)