Skip to content

Commit 62c8f2f

Browse files
committed
Issue #7 Make writer object accessible
1 parent 7959054 commit 62c8f2f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ Find more examples below.
4747

4848
Property | Description
4949
---------|-------------
50-
`writer` | The file format as supported by PHPOffice. The default is ` '\PHPExcel_Writer_Excel2007'`
50+
`writerClass` | The file format as supported by PHPOffice. The default is ` '\PHPExcel_Writer_Excel2007'`
5151
`sheets` | An array of sheet configurations (see below). The keys are used as sheet names.
5252

5353
Methods | Description
5454
---------|-------------
5555
`saveAs($name)` | Saves the excel file under `$name`
5656
`send($name=null,$inline=false)` | 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.
57+
`getWriter()` | Returns the `PHPExcel_Writer_Abstract` instance
5758
`getWorkbook()` | Returns the `PHPExcel` workbook instance
5859
`getTmpFile()` | Returns the `mikehaertl\tmp\File` instance of the temporary file
5960

@@ -93,7 +94,7 @@ Property | Description
9394
$file = \Yii::createObject([
9495
'class' => 'codemix\excelexport\ExcelFile',
9596

96-
'writer' => '\PHPExcel_Writer_Excel5', // Override default of `\PHPExcel_Writer_Excel2007`
97+
'writerClass' => '\PHPExcel_Writer_Excel5', // Override default of `\PHPExcel_Writer_Excel2007`
9798

9899
'sheets' => [
99100

src/ExcelFile.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ class ExcelFile extends Object
1313
/**
1414
* @var string the writer class to use. Default is `\PHPExcel_Writer_Excel2007`.
1515
*/
16-
public $writer = '\PHPExcel_Writer_Excel2007';
16+
public $writerClass = '\PHPExcel_Writer_Excel2007';
1717

18+
protected $_writer;
1819
protected $_workbook;
1920
protected $_sheets;
2021
protected $_tmpFile;
2122
protected $_created = false;
2223

24+
/**
25+
* @return PHPExcel_Writer_Abstract the writer instance
26+
*/
27+
public function getWriter()
28+
{
29+
if ($this->_writer===null) {
30+
$class = $this->writerClass;
31+
$this->_writer = new $class($this->getWorkbook());
32+
}
33+
return $this->_writer;
34+
}
35+
2336
/**
2437
* @return PHPExcel the workbook instance
2538
*/
@@ -108,9 +121,7 @@ protected function createFile()
108121
}
109122
Yii::createObject($config, [$sheet])->render();
110123
}
111-
$class = $this->writer;
112-
$writer = new $class($workbook);
113-
$writer->save((string) $this->getTmpFile());
124+
$this->getWriter()->save((string) $this->getTmpFile());
114125
$this->_created = true;
115126
}
116127
}

0 commit comments

Comments
 (0)