Skip to content

Commit d430c46

Browse files
authored
Merge pull request #44 from devOp1/master
Replaced PhpExcel with PhpSpreadsheet
2 parents d62aaad + 8c9c5da commit d430c46

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Yii2 Excel Export
1616
* Create excel files with multiple sheets
1717
* Format cells and values
1818

19-
To write the Excel file, we use the excellent [PHPExcel](https://github.com/PHPOffice/PHPExcel) package.
19+
To write the Excel file, we use the excellent [PHPSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) package.
2020

2121
## Installation
2222

@@ -49,7 +49,7 @@ Find more examples below.
4949

5050
Property | Description
5151
---------|-------------
52-
`writerClass` | The file format as supported by PHPOffice. The default is `\PHPExcel_Writer_Excel2007`
52+
`writerClass` | The file format as supported by PHPOffice. The default is `\PhpOffice\PhpSpreadsheet\Writer\Xlsx`
5353
`sheets` | An array of sheet configurations (see below). The keys are used as sheet names.
5454
`fileOptions` | Options to pass to the constructor of `mikehaertl\tmp\File`. Available keys are `prefix`, `suffix` and `directory`.
5555

@@ -58,8 +58,8 @@ Methods | Description
5858
`saveAs($name)` | Saves the excel file under `$name`
5959
`send($name=null, $inline=false, $contentType = 'application/vnd.ms-excel')` | Sends the excel file to the browser. If `$name` is empty, the file is streamed for inline display, otherwhise a download dialog will open, unless `$inline` is `true` which will force inline display even if a filename is supplied.
6060
`createSheets()` | Only creates the sheets of the excel workbook but does not save the file. This is usually called implicitely on `saveAs()` and `send()` but can also be called manually to modify the sheets before saving.
61-
`getWriter()` | Returns the `PHPExcel_Writer_Abstract` instance
62-
`getWorkbook()` | Returns the `PHPExcel` workbook instance
61+
`getWriter()` | Returns the `\PhpOffice\PhpSpreadsheet\Writer\BaseWrite` instance
62+
`getWorkbook()` | Returns the `\PhpOffice\PhpSpreadsheet\Spreadsheet` workbook instance
6363
`getTmpFile()` | Returns the `mikehaertl\tmp\File` instance of the temporary file
6464

6565
### ExcelSheet
@@ -68,11 +68,11 @@ Property | Description
6868
---------|-------------
6969
`data` | An array of data rows that should be used as sheet content
7070
`titles` (optional) | An array of column titles
71-
`types` (optional) | An array of types for specific columns as supported by PHPOffice, e.g. `\PHPExcel_Cell_DataType::TYPE_STRING`, indexed either by column name (e.g. `H`) or 0-based column index.
71+
`types` (optional) | An array of types for specific columns as supported by PHPOffice, e.g. `DataType::TYPE_STRING`, indexed either by column name (e.g. `H`) or 0-based column index.
7272
`formats` (optional) | An array of format strings for specific columns as supported by Excel, e.g. `#,##0.00`, indexed either by column name (e.g. `H`) or 0-based column index.
7373
`formatters` (optional) | An array of value formatters for specific columns. Each must be a valid PHP callable whith the signature `function formatter($value, $row, $data)` where `$value` is the cell value to format, `$row` is the 0-based row index and `$data` is the current row data from the `data` configuration. The callbacks must be indexed either by column name (e.g. `H`) or by the 0-based column index.
7474
`styles` (optional) | An array of style configuration indexed by cell coordinates or a range.
75-
`callbacks` (optional) | An array of callbacks indexed by column that should be called after rendering a cell, e.g. to apply further complex styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PHPExcel_Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.
75+
`callbacks` (optional) | An array of callbacks indexed by column that should be called after rendering a cell, e.g. to apply further complex styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PhpOffice\PhpSpreadsheet\Cell\Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.
7676
`startColumn` (optional) | The start column name or its 0-based index. When this is set, the 0-based offset is added to all numeric keys used anywhere in this class. Columns referenced by name will stay unchanged. Default is 'A'.
7777
`startRow` (optional) | The start row. Default is 1.
7878

@@ -116,7 +116,7 @@ Property | Description
116116
$file = \Yii::createObject([
117117
'class' => 'codemix\excelexport\ExcelFile',
118118

119-
'writerClass' => '\PHPExcel_Writer_Excel5', // Override default of `\PHPExcel_Writer_Excel2007`
119+
'writerClass' => '\PhpOffice\PhpSpreadsheet\Writer\Xls', // Override default of `\PhpOffice\PhpSpreadsheet\Writer\Xlsx`
120120

121121
'sheets' => [
122122

@@ -177,7 +177,7 @@ $file = \Yii::createObject([
177177
'formatters' => [
178178
// Dates and datetimes must be converted to Excel format
179179
3 => function ($value, $row, $data) {
180-
return \PHPExcel_Shared_Date::PHPToExcel(strtotime($value));
180+
return \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel(strtotime($value));
181181
},
182182
],
183183
],
@@ -215,7 +215,7 @@ $file->send('demo.xlsx');
215215

216216
Since version 2.3.0 you can style single cells and cell ranges via the `styles`
217217
property of a sheet. For details on the accepted styling format please consult the
218-
[PHPExcel documentation](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#styles).
218+
[PhpSpreadsheet documentation](https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet-style.html).
219219

220220
```php
221221
<?php
@@ -234,7 +234,7 @@ $file = \Yii::createObject([
234234
'name' => 'Verdana'
235235
],
236236
'alignment' => [
237-
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
237+
'horizontal' => Alignment::HORIZONTAL_RIGHT,
238238
],
239239
],
240240
],
@@ -256,7 +256,7 @@ $file
256256
->getStyle('B1')
257257
->getFont()
258258
->getColor()
259-
->setARGB(\PHPExcel_Style_Color::COLOR_RED);
259+
->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
260260
$file->send();
261261
```
262262

@@ -271,27 +271,27 @@ $file = \Yii::createObject([
271271
'class' => 'codemix\excelexport\ActiveExcelSheet',
272272
'query' => User::find(),
273273
'callbacks' => [
274-
// $cell is a PHPExcel_Cell object
274+
// $cell is a \PhpOffice\PhpSpreadsheet\Cell object
275275
'A' => function ($cell, $row, $column) {
276276
$cell->getStyle()->applyFromArray([
277277
'font' => [
278278
'bold' => true,
279279
],
280280
'alignment' => [
281-
'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
281+
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
282282
],
283283
'borders' => [
284284
'top' => [
285-
'style' => \PHPExcel_Style_Border::BORDER_THIN,
285+
'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
286286
],
287287
],
288288
'fill' => [
289-
'type' => \PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
289+
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
290290
'rotation' => 90,
291-
'startcolor' => [
291+
'startColor' => [
292292
'argb' => 'FFA0A0A0',
293293
],
294-
'endcolor' => [
294+
'endColor' => [
295295
'argb' => 'FFFFFFFF',
296296
],
297297
],

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
}
1212
],
1313
"require": {
14+
"php": ">=7.4",
1415
"yiisoft/yii2": "~2.0.13",
1516
"mikehaertl/php-tmpfile": "^1.0.0",
16-
"phpoffice/phpexcel": "1.*"
17+
"phpoffice/phpspreadsheet": "^1.25.2"
1718
},
1819
"autoload": {
1920
"psr-4": {

src/ActiveExcelSheet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function getFormatters()
204204
date_default_timezone_set(Yii::$app->formatter->defaultTimeZone);
205205
$timestamp = strtotime($v);
206206
date_default_timezone_set($timezone);
207-
return \PHPExcel_Shared_Date::PHPToExcel($timestamp);
207+
return \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($timestamp);
208208
}
209209
};
210210
break;
@@ -213,7 +213,7 @@ public function getFormatters()
213213
if (empty($v)) {
214214
return null;
215215
} else {
216-
return \PHPExcel_Shared_Date::PHPToExcel($this->toExcelTime($v));
216+
return \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($this->toExcelTime($v));
217217
}
218218
};
219219
break;

src/ExcelFile.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class ExcelFile extends BaseObject
1313
{
1414
/**
1515
* @var string the writer class to use. Default is
16-
* `\PHPExcel_Writer_Excel2007`.
16+
* `\PhpOffice\PhpSpreadsheet\Writer\Xlsx`.
1717
*/
18-
public $writerClass = '\PHPExcel_Writer_Excel2007';
18+
public $writerClass = '\PhpOffice\PhpSpreadsheet\Writer\Xlsx';
1919

2020
/**
2121
* @var array options to pass to the constructor of \mikehaertl\tmp\File,
@@ -33,7 +33,7 @@ class ExcelFile extends BaseObject
3333
protected $_sheetsCreated = false;
3434

3535
/**
36-
* @return PHPExcel_Writer_Abstract the writer instance
36+
* @return \PhpOffice\PhpSpreadsheet\Writer\BaseWriter the writer instance
3737
*/
3838
public function getWriter()
3939
{
@@ -45,12 +45,12 @@ public function getWriter()
4545
}
4646

4747
/**
48-
* @return PHPExcel the workbook instance
48+
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet the workbook instance
4949
*/
5050
public function getWorkbook()
5151
{
5252
if ($this->_workbook === null) {
53-
$this->_workbook = new \PHPExcel();
53+
$this->_workbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
5454
}
5555
return $this->_workbook;
5656
}

src/ExcelSheet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExcelSheet extends Component
3535
protected $_row;
3636

3737
/**
38-
* @param PHPExcel_WorkSheet $sheet
38+
* @param \PhpOffice\PhpSpreadsheet\Worksheet $sheet
3939
* @param array $config
4040
*/
4141
public function __construct($sheet, $config = [])
@@ -45,7 +45,7 @@ public function __construct($sheet, $config = [])
4545
}
4646

4747
/**
48-
* @return PHPExcel_WorkSheet
48+
* @return \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
4949
*/
5050
public function getSheet()
5151
{
@@ -312,7 +312,7 @@ protected function normalizeIndex($data)
312312
public function normalizeColumn($column)
313313
{
314314
if (is_string($column)) {
315-
return \PHPExcel_Cell::columnIndexFromString($column) - 1;
315+
return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($column) - 1;
316316
} else {
317317
return $column + self::normalizeColumn($this->startColumn);
318318
}

0 commit comments

Comments
 (0)